字符串⽐较函数strcmp_C程序使⽤strcmp()函数⽐较字符
串
字符串⽐较函数strcmp
Given two strings and we have to compare them using strcmp() function in C language.
给定两个字符串,我们必须使⽤C语⾔的strcmp()函数进⾏⽐较。
C语⾔strcmp()函数 (C language strcmp() function)
strcmp() function is used to compare two stings, it checks whether two strings are equal or not.
strcmp() function
strcmp()函数
strcmp()函数⽤于⽐较两个字符串,它检查两个字符串是否相等。
strcmp() function checks each character of both the strings one by one and calculate the difference of the of the both of strcmp() function
the characters. This process continues until a difference of non-zero occurs or a \0 character is reached.
strcmp()函数⼀个接⼀个地检查两个字符串的每个字符,并计算两个字符的之差。 此过程⼀直持续到出现⾮零的差异或达到\ 0字符为strcmp()函数
⽌。
Return value/result of strcmp() function:
返回值/ strcmp()函数的结果:
0 : If both the strings are equal
0 :如果两个字符串相等
Negative : If the ASCII value of first unmatched character of first string is smaller than the second string
Negative
负数 :如果第⼀个字符串的第⼀个不匹配字符的ASCII值⼩于第⼆个字符串
负数
Positive : If the ASCII value of first unmatched character of first character is greater than the second string
Positive
正数
正数 :如果第⼀个字符的第⼀个不匹配字符的ASCII值⼤于第⼆个字符串
C程序/ strcmp()函数的⽰例 (C program/example for strcmp() function)
#include <stdio.h>
#include <string.h>
int main(){
char str1[] = "Includehelp", str2[] = "includehelp", str3[] = "Includehelp"; int res = 0, cmp = 0;
// Compares string1 and string2 and return the difference
// of first unmatched character in both of the strings
// unless a '\0'(null) character is reached.
res = strcmp(str1, str2);
if (res == 0)
printf("\n %s and %s are equal\n\n", str1, str2);
else
printf("\n %s and %s are not equal\n\n", str1, str2);
// Compares string1 and string3 and return the difference
// of first unmatched character in both of the strings
// unless a '\0'(null) character is reached.
cmp = strcmp(str1, str3);
if (cmp == 0)
printf(" %s and %s are equal\n\n", str1, str3);
else
printf(" %s and %s are not equal\n\n", str1, str3);
return 0;
}
Output
输出量
Includehelp and includehelp are not equal
Includehelp and Includehelp are equal
字符串⽐较函数strcmp
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论