strcoll()函数类似于strcmp()函数,它比较两个字符串并根据比较结果返回一个整数。
C strcoll()声明
int strcoll(const char *str1, const char *str2)
str1 – 第一个字符串
str2 – 第二个字符串
strcoll()的返回值
> 0:如果字符串str1中第一个不匹配字符的 ASCII 值大于str2。< 0:如果字符串str1中第一个不匹配字符的 ASCII 值小于str2。= 0:如果两个字符串相等C strcoll()函数示例
#include <stdio.h>#include <string.h>int main () { char str1[20]; char str2[20]; int result; strcpy(str1, "HELLO"); strcpy(str2, "hello world!"); result = strcoll(str1, str2); if(result > 0) { printf("ASCII value of first unmatched character of str1 is greater than str2"); } else if(result < 0) { printf("ASCII value of first unmatched character of str1 is less than str2"); } else { printf("Both the strings str1 and str2 are equal"); } return 0;}输出:
ASCII value of first unmatched character of str1 is less than str2
在这个例子中,我们比较两个不同的字符串。strcoll()函数区分大小写。 'H'的 ASCII 值小于'h',这就是此函数返回负值的原因。
如果我们反转strcoll()函数中的参数,让我们说strcoll(str2, str1),那么函数将返回一个正值。
