The strcoll() function is similar to strcmp() function, it compares two strings and returns an integer number based on the result of comparison.
C strcoll() declaration
int strcoll(const char *str1, const char *str2)
str1 – First String
str2 – Second String
Return value of strcoll()
- > 0 if the ASCII value of first unmatched character in string str1 is greater than str2.
- < 0 if the ASCII value of first unmatched character in string str1 is less than str2.
- =0 if both strings are equal
C strcoll() function example
#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; }
Output:
ASCII value of first unmatched character of str1 is less than str2
In this example we are comparing two strings that are different. The strcoll() function is case sensitive. The ASCII value of ‘H’ is less than ‘h’ which is why this function returns negative value.
If we reverse the arguments in the strcoll() function, lets say strcoll(str2, str1) then the function would return a positive value.
Leave a Reply