strcoll()是一个内置库函数,在<string.h>头文件中声明。此函数将str1 指向的字符串与str2指向的字符串进行比较。strcoll()函数根据当前语言环境的LC_COLLATE类别的规则执行比较。
语法:
int strcoll(const char *str1, const char *str2)
参数:函数 strcoll() 接受两个字符串作为参数并返回一个整数值。
值 含义 小于零 str1小于str2 零 str1等于str2 大于零 str1大于str2
- 小于零:当str1小于str2
// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10];
char str2[10];
int ret;
strcpy(str1, "cxydh");
strcpy(str2, "CXYDH");
ret = strcoll(str1, str2);
if (ret > 0) {
printf("str1 is greater than str2");
} else if (ret < 0) {
printf("str1 is lesser than str2");
} else {
printf("str1 is equal to str2");
}
return (0);
}
输出:
str1 is greater than str2
- 大于零:当str1大于str2
// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10];
char str2[10];
int ret;
strcpy(str1, "CXYDH");
strcpy(str2, "cxydh");
ret = strcoll(str1, str2);
if (ret > 0) {
printf("str1 is greater than str2");
} else if (ret < 0) {
printf("str1 is lesser than str2");
} else {
printf("str1 is equal to str2");
}
return (0);
}
输出:
str1 is lesser than str2
- 等于零:当str1等于str2
// C program to illustrate strcoll()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10];
char str2[10];
int ret;
strcpy(str1, "CXYDH");
strcpy(str2, "CXYDH");
ret = strcoll(str1, str2);
if (ret > 0) {
printf("str1 is greater than str2");
} else if (ret < 0) {
printf("str1 is lesser than str2");
} else {
printf("str1 is equal to str2");
}
return (0);
}
输出:
str1 is equal to str2