strnicmp() — Compare Substrings Without Case Sensitivity
Format
#include <string.h>
int strnicmp(const char *string1, const char *string2, int n);
Note: The
strnset
and strset
functions
are available for C++ programs. They are available for C only when
the program defines the __cplusplus__strings__ macro.Language Level
Extension
Threadsafe
Yes
Locale Sensitive
The behavior of this function might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.
Description
The strnicmp()
function compares,
at most, the first n characters of string1 and string2 without
sensitivity to case.
The function operates on null terminated strings. The string arguments to the function are expected to contain a null character (\0) marking the end of the string.
Return Value
The strnicmp()
function returns
a value indicating the relationship between the substrings, as follows:
Value | Meaning |
---|---|
Less than 0 | string1 less than string2 |
0 | string1 equivalent to string2 |
Greater than 0 | string1 greater than string2 |
Example
This example uses
strnicmp()
to
compare two strings. #include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "THIS IS THE FIRST STRING";
char *str2 = "This is the second string";
int numresult;
/* Compare the first 11 characters of str1 and str2
without regard to case */
numresult = strnicmp(str1, str2, 11);
if (numresult < 0)
printf("String 1 is less than string2.\n");
else
if (numresult > 0)
printf("String 1 is greater than string2.\n");
else
printf("The two strings are equivalent.\n");
return 0;
}
The output should be: The two strings are equivalent.