Programming Reference/Librarys
Question & Answer
Q&A is closed
#include <stdlib.h> int strncmp(const char *str1, const char *str2, size_t n);
The strcmp() function compares the two strings s1
and s2
. It returns an integer less than, equal to, or greater than zero if s1
is found, respectively, to be less than, to match, or be greater than s2
. The strncmp() function is similar, except it only compares the first (at most) n characters of s1
and s2
.
/* * strncmp example code * http://code-reference.com/c/string.h/strncmp */ #include <stdio.h> #include <string.h> int main( void ) { const char str1[] = "42 is a bad answer"; const char str2[] = "42 is a realy good answer"; int i; i = strlen(str1); for(i ; i > 0; i--) { if(strncmp( str1, str2, i) == 0) { printf("The first %d chars of str1 and str2 are the same\n", i); break; } } return 0; } }
The first 8 chars of str1 and str2 are the same