#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    strcasecmp does not work


    Hello,

    I hope someone is able to help me with this.

    my strcasecmp (also tried strcmp), does not work even though the two values appear the same.

    students is an array of structures where school is assigned as "char school[40].

    school(assigned char school[40]) is and varialble from the function and is an input from the user(using gets).

    When i print of the two values they look the same, but when i use strlen() the length is not the same.. It belive the problem might be how the value is stored in the structure..?

    this is the code:

    for(i=0; i < noRecords ;i++) {

    if (strcasecmp(students[i].school, school) == 0 ) {
    schoolFound = 1;
    printf("FOUND\n");
    noStudents++;
    } else {
    printf("Not found\n");
    }
    }

    Does anyone have any ideas why this does not work?

    Help is appreciated!! ;)
    Last edited by kfrederi; August 31st, 2003 at 08:49 AM.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Please enclose your code in "code" brackets.

    This works for me:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NUMRECS 5
    
    struct STUDS{
        char school[40];
    }students[NUMRECS];
    
    int main(){
        int i, noRecords = NUMRECS, noStudents = 0, schoolFound = 0;
        char school[] = "Harvard";
    
        i = 0;
        strcpy(students[i++].school, "UVA");
        strcpy(students[i++].school, "Harvard");
        strcpy(students[i++].school, "Vajinyatek");
        strcpy(students[i++].school, "Geojatek");
        strcpy(students[i++].school, "MIT");
    
        for(i=0; i < noRecords ;i++) { 
            if (strcmp(students[i].school, school) == 0 ) { 
                schoolFound = 1; 
                printf("Found a match for %s\n", students[i].school);
                noStudents++; 
            } else { 
                printf("No match for %s\n", students[i].school); 
            } 
        }
    
        if (schoolFound){
            printf("Found a match, noStudents = %d\n", noStudents);
        }
    
        return 0;
    }
    Output:

    Code:
    No match for UVA
    Found a match for Harvard
    No match for Vajinyatek
    No match for Geojatek
    No match for MIT
    Found a match, noStudents = 1

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    The user input may include the carriage-return character before the NUL terminator (it depends on what function you have used for the user input).

    You can test this by:

    Code:
    printf( "%s%s", school, school ) ;
    The two duplcate strings will appear on separate lines if there is a carriage-return or new-line appended to the string.

    If this is the case you could do one of the following:


    Code:
    school[strlen(school) - 1] = '\0' ;  // replace last-but-one char with NUL
    
    school[strcspn( school, "\r\n\0" )] = '\0' ; // replace first occurance of \n, \r, or \0 with NUL
    The second option is more robust, but may be unnecessary. A better approach would be to use an input function that strips the CR.


    Clifford.
    Last edited by clifford; August 31st, 2003 at 12:51 PM.

IMN logo majestic logo threadwatch logo seochat tools logo