Can't string compare

This is a discussion on Can't string compare within the C Programming forums, part of the General Programming Boards category; Our aim is to create a palindrome program.for example input Never odd or even and output This is a palindrome.This ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26

    Can't string compare

    Our aim is to create a palindrome program.for example input Never odd or even and output This is a palindrome.This is what i have done so far but i am stuck in string compare m=strcmp(a[0],a[1]);.Thanks in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    
    char * remSpaces(char *ch){
    char *ch2 = ch;
    int i,j=0;
    for(i=0;i<=strlen(ch);i++){
    if(ch[i]!=' '){
    ch2[j] = ch[i];
    j++;
    }
    }
    
    return ch2;
    
    }
    
    
    
    
    int main()
    {
        int m;
        int i;
        char*a;
        char *ch = malloc(256);
    
        gets(ch);
    
    
    
        a=remSpaces(ch);
        printf("%s",a);
        m=strcmp(a[0],a[1]);
    
    
    
    
    
    return 0;
    }

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    a[0] and a[1], in your main() function are chars, not strings.
    You cannot call strcmp() with chars.
    Either make a[0] and a[1] strings (this is wrong for your problem) or use another comparison method (comparing the chars themselves).

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    Quote Originally Posted by qny View Post
    a[0] and a[1], in your main() function are chars, not strings.
    You cannot call strcmp() with chars.
    Either make a[0] and a[1] strings (this is wrong for your problem) or use another comparison method (comparing the chars themselves).
    So why this doesnt work?

    Code:
    int main()
    {
        int m=0;
        int i;
    
        char *ch = malloc(256);
    
        gets(ch);
    
    
    
        char*a=remSpaces(ch);
        printf("%s",a);
         for (i=0;i<=strlen(a);i++){
          if (a[i]!=a[i-strlen(a)]){
           printf("5");
    
          }
    
    
        }
    
    
    
    
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    a[i-strlen(a)]

    first time through the loop, i=strlen(a) is a negative number.
    also, "Never odd or even" is not a palindrome because 'N' != 'n'.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Heraklion, Greece, Greece
    Posts
    26
    they told us to use toupper()..

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Καλησπερα συντεκνε, αν και δεν ειμαι κρητικος!

    toupper just converts the letter to an uppercase letter.

    Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows). So use fgets instead

    Also notice that m is an unused variable.

    When i want to compare a string i do this
    Code:
    #include <stdio.h>
    
    int main(void)
    {
         char* str1 = "Greece";
        char* str2 = "Crete";
    
        char c1 = 'a';
        char c2 = 'b';
    
        if(strcmp(str1,str2)==0) {
            printf("Strings are the same!!\n");
        }
        else {
            printf("Strings are NOT the same!!\n");
        }
    
        if(c1 == c2) {
             printf("c1 and c2 are the same!!\n");
        }
        else {
             printf("c1 and c2 are NOT the same!!\n");
        }
    
        /* Because str1 and str2 are pointers to char, we can handle them as  */
        /* arrays as well.Do not make the mistake to consider that pointers   */
        /* are the same as arrays.They have some common properties, but there */
        /* are not equivalent                                                 */
        if(str1[2] == str2[2]) {
             printf("Third letter of Greece and Crete are the same!\n");
        }
        else {
             printf("We did something wrong!\n");
        }
        return 0;
    }
    Hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string compare
    By ingeniousreader in forum C Programming
    Replies: 7
    Last Post: 03-02-2012, 11:43 AM
  2. String Compare
    By shruthi in forum C Programming
    Replies: 2
    Last Post: 02-09-2012, 08:27 AM
  3. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  4. string compare
    By IceBall in forum C Programming
    Replies: 4
    Last Post: 10-12-2003, 05:26 PM
  5. need help on string compare
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-07-2002, 08:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21