Name
strcmp, strncmp
- compare two strings
Library
libc.lib
Synopsis
|
int
strcmp (const char *s1, const char *s2);
|
|
int
strncmp (const char *s1, const char *s2, size_t len);
|
Return values
The
strcmp
and
strncmp
return an integer greater than, equal to, or less than 0, according
as the string
s1
is greater than, equal to, or less than the string
s2.
Detailed description
The
strcmp
and
strncmp
functions
lexicographically compare the null-terminated strings
s1
and
s2.
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.
Examples
#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.
See also
bcmp,
memcmp,
strcasecmp,
strcoll,
strxfrm
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|