C语言 strcoll()函数

来源:这里教程网 时间:2026-02-16 12:53:59 作者:

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),那么函数将返回一个正值。

相关文章:

    C – strcat()函数C – strchr()函数C – strncat()函数C – strncmp()函数

相关推荐