Example: strnicmp

The following example compares two strings, string1 and string2, which are nearly identical - string2 contains the word "one" instead of "the" and the word "dog" in uppercase instead of lowercase.

stricmp returns an unequal comparison because it compares the whole string which includes the word imbalance "one" and "the". strnicmp compares 30 characters only, which falls short of the word imbalance in the strings. strnicmp also ignores the uppercase "DOG". It therefore returns an equal comparison.

    int result;
    char tmp[20];
    char string1[] = "The quick brown dog jumps over the lazy fox";
    char string2[] = "The quick brown DOG jumps over one lazy fox";
// Perform a case sensitive comparison 
    result = stricmp( string1, string2 ); 
    if (result > 0)
        strcpy(tmp, "greater than");
    else if (result < 0)
        strcpy(tmp, "less than");
    else
        strcpy(tmp, "equal to");
    lr_output_message ("stricmp: String 1 is %s string 2", tmp);
// Compare 30 chars  
    result = strnicmp( string1, string2 , 30); 
    if (result > 0 )
        strcpy( tmp, "greater than");
    else if (result < 0)
        strcpy(tmp, "less than"); 
    else
        strcpy(tmp, "equal to");
    lr_output_message ("strnicmp: String 1 is %s string 2", tmp);
Example: Output:
Action.c(17): stricmp: String 1 is greater than string 2
Action.c(28): strnicmp: String 1 is equal to string 2