strnicmp

String Manipulation Functions

Performs a case-insensitive comparison between n characters from string1 and string2 to determine the alphabetic order.

int strnicmp( constchar *string1, const char *string2, size_t num);

string1 The first string that is compared.
string2 The second string that is compared.
num The number of characters to compare.

Return Values

Returns a value indicating the lexicographical relation between the strings:

Return value Description
<0 string1 is less than string2
0 string1 is the same as string2
>0 string1 is greater than string2

Example

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