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

    Join Date
    Aug 2003
    Rep Power

    strcasecmp does not work


    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;
    } 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
    Rep Power
    Please enclose your code in "code" brackets.

    This works for me:

    #include <stdio.h>
    #include <string.h>
    #define NUMRECS 5
    struct STUDS{
        char school[40];
    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");
            if (strcmp(students[i].school, school) == 0 ) { 
                schoolFound = 1; 
                printf("Found a match for %s\n", students[i].school);
            } else { 
                printf("No match for %s\n", students[i].school); 
        if (schoolFound){
            printf("Found a match, noStudents = %d\n", noStudents);
        return 0;

    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

    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    --Me, I just made it up

    --George Bernard Shaw
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Rep Power
    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:

    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:

    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.

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

IMN logo majestic logo threadwatch logo seochat tools logo