C examples for string.h:strncmp
function
<cstring> <string.h>
Compares up to num characters of the C string str1 to those of the C string str2.
int strncmp ( const char * str1, const char * str2, size_t num );
Parameter | Description |
---|---|
str1 | C string to be compared. |
str2 | C string to be compared. |
num | Maximum number of characters to compare. |
Returns an integral value indicating the relationship between the strings:
return value | indicates |
---|---|
<0 | the first character has a lower value in str1 than in str2 |
0 | the contents of both strings are equal |
>0 | the first character has a greater value in str1 than in str2 |
#include <stdio.h> #include <string.h> int main()/*w w w .j a v a 2 s .c om*/ { char str[][10] = { "test" , " test" , "test " }; int n; for (n = 0; n<3; n++) { if (strncmp(str[n], "texx", 2) == 0) { printf("found %s\n", str[n]); } } return 0; }