c语⾔strcmp函数⽤法_strcmp()C库函数⽤法和⽰例
c语⾔strcmp函数⽤法
C programming standard library provides strcmp() function in order to compare two strings and return the results
whether they are identical or different.
C编程标准库提供strcmp()函数,以便⽐较两个字符串并返回相同或不同的结果。
语法和参数 (Syntax and Parameters)
As stated previously strcmp() function takes two char array or string arguments.
如前所述,strcmp()函数采⽤两个char数组或字符串参数。
int strcmp (const char* str1, const char* str2);
const char* str1 is the first string or char array which will be compared the second one. const is mainly used to prevent given char array pointer to be changed.
const char * str1是第⼀个字符串或char数组,将与第⼆个字符串或char数组进⾏⽐较。 const主要⽤于防⽌更改给定的char数组指针。
const char* str2 is the second string or char array which will be compared with the first one.
const char * str2是第⼆个字符串或char数组,将与第⼀个⽐较。
返回值 (Return Values)
strcmp() function returns an int or integer type. We can get 3 types of return value that are explained below.
strcmp()函数返回⼀个int或整数类型。 我们可以得到以下三种返回值类型。c++中string的用法
`0` is returned if both strings are identical, equal or the same.
如果两个字符串相同,相等或相同,则返回“ 0”。
`Negative Integer` if the ASCII value of the first unmatched character is less then second
“负整数”,如果第⼀个不匹配字符的ASCII值⼩于第⼆个字符
`Positive Integer` if the ASCII value of the first unmatched character is greater than second
如果第⼀个不匹配字符的ASCII值⼤于第⼆个,则为“正整数”
⽐较两个字符串 (Compare Two Strings)
We can compare two strings which are expressed as char array in C programming language. We will compare strings “I love poftut” and “I loves poftut” .
我们可以⽐较两个⽤C编程语⾔表⽰为char数组的字符串。 我们将⽐较字符串“我爱poftut”和“我爱poftut”。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "I love poftut", str2[] = "I loves poftut";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
return 0;
}
We will compile with gcc like below and then run the binary.
我们将使⽤下⾯的gcc进⾏编译,然后运⾏⼆进制⽂件。
$ gcc strcmp.c -o strcmp
$ ./strcmp
Compare Two Strings
⽐较两个字符串
⽐较两个字符数组(Compare Two Char Array)
We can compare two char arrays where
我们可以⽐较两个字符数组
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "I love poftut", str2[] = "I love poftut";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
return 0;
}
在C和C ++教程中了解更多strstr()函数的⽰例
Compare Two Char Array
⽐较两个字符数组
We can see that both of the character arrays are the same so the return value will be 0 and printed to the screen.
我们可以看到两个字符数组都相同,因此返回值将为0并打印到屏幕上。
Compare Two Char Array
⽐较两个字符数组
字符串不同,第⼀个更⼤,并返回正值(Strings Are Different and First Is Bigger and Return Positive Value)
In this example, the first string is bigger and return a positive integer which is the value of the character.
在此⽰例中,第⼀个字符串较⼤,并返回⼀个正整数,该整数是字符的值。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "aBcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
return 0;
}
Strings Are Different and First Is Bigger and Return Positive Value
字符串不同,第⼀个更⼤,并返回正值
字符串不同,第⼆个更⼤,返回负值(Strings Are Different and Second Is Bigger Return Negative Value)
In this example, the second string is bigger and return a positive integer which is the value of the character.
在此⽰例中,第⼆个字符串更⼤,并返回⼀个正整数,该整数是字符的值。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "aBcd", str2[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
}
Strings Are Different and Second Is Bigger Return Negative Value
字符串不同,第⼆个更⼤,返回负值
字符串相同返回0(Strings Are Same Return 0)
If both strings are the same the strcmp() function will return zero 0 .
如果两个字符串相同,则strcmp()函数将返回零0。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
return 0;
}
Strings Are Same Return 0
字符串相同返回0
c语⾔strcmp函数⽤法

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。