#include <stdio. h>
#include <string. h>
void main (void)
void main(void)
{
char* str_1 = "abc" char * str_2 = "abc" char* str_3 = "ABC"
if (strcmp(str_1, str_2) == 0)
printf("str_1 is equal to str_2. \n")
else
printf("str_1 is not equal to str_2. \n")
if (strcmp(str_1, str_3) == 0)
printf("str_1 is equal to str_3.\n")
else
printf("str_1 is not equalto str_3.\n");
}
上例的打印输出如下所示:
str_1 is equal to str_2.
str_1 is not equal to str_3.
strcmp()函数有两个参数,即要比较的两个字符串。strcmp()函数对两个字符串进行大小写敏感的(case-sensitiVe)和字典式的(lexicographic)比较,并返回下列值之一:
----------------------------------------------------
返 回 值 意 义
----------------------------------------------------
<0 第一个字符串小于第二个字符串
0 两个字符串相等·
>0 第一个字符串大于第二个字符串
----------------------------------------------------
在上例中,当比较str_1(即“abc”)和str_2(即“abc”)时,strcmp()函数的返回值为0。然而,当比较str_1(即"abc")和str_3(即"ABC")时,strcmp()函数返回一个大于0的值,因为按ASCII顺序字符串“ABC”小于“abc”。
strcmp()函数有许多变体,它们的基本功能是相同的,都是比较两个字符串,但其它地方稍有差别。下表列出了C语言提供的与strcmp()函数类似的一些函数:
-----------------------------------------------------------------
函 数 名 作 用
-----------------------------------------------------------------
strcmp() 对两个字符串进行大小写敏感的比较
strcmpi()对两个字符串进行大小写不敏感的比较
stricmp()同strcmpi()
strncmp()对两个字符串的一部分进行大小写敏感的比较
strnicmp() 对两个字符串的一部分进行大小写不敏感的比较
-----------------------------------------------------------------
在前面的例子中,如果用strcmpi()函数代替strcmp()函数,则程序将认为字符串“ABC”等于“abc”。
C语言中用strcmp函数比较字符和字符串是否相等。
strcmp函数功能是比较两个字符串的大小,返回比较的结果。
函数形式:strcmp(str1,str2)
参数说明:str1,str2可以是字符串常量或变量。
结果说明:
①返回值小于0,说明str1小于str2。
②返回值等于0,说明str1与str2相等。
③返回值大于0,s说明str1大于str2。