#include <string.h>
|
int
strcmp (const char *s1, const char *s2); |
int
strncmp (const char *s1, const char *s2, size_t len); |
The strncmp function compares not more than len characters. Because strncmp is designed for comparing strings rather than binary data, characters that appear after a \0 character are not compared.
#include <string.h> #include <stdio.h> int main() { char str1[] = "abcdefg"; char str2[] = "abcdefr"; int result; printf( "Compare '%s' to '%s\n", str1, str2 ); result = strcmp( str1, str2); if( result < 0 ) printf( "str1 is less than str2.\n" ); else if( result == 0 ) printf( "str1 is equal to str2.\n" ); else if( result > 0 ) printf( "str1 is greater than str2.\n" ); printf( "Compare '%.6s' to '%.6s\n", str1, str2 ); result = strncmp( str1, str2, 6 ); if( result < 0 ) printf( "str1 is less than str2.\n" ); else if( result == 0 ) printf( "str1 is equal to str2.\n" ); else if( result > 0 ) printf( "str1 is greater than str2.\n" ); return 0; }
Output
Compare abcdefg to abcdefr str1 is less than str2. Compare abased to abcdef str1 is equal to str2.
© 2007-2009 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user. |
|