
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - strcoll() function
The C library strcoll() function accepts two pointer variable which compares two strings say(str1 to str2). The result is dependent on the LC_COLLATE setting of the location.
Its ability is to compare the strings in locale manner. The string comparison functions, such as strcmp, perform a simple byte-to-byte comparison, while strcoll() takes into account the current locale settings. This allows for string comparison based on important rules such as alphabetical order and case sensitivity.
Syntax
Following is the syntax of the C library function strcoll() −
strcoll(const char *str1, const char *str2)
Parameters
This function accepts the following parameters −
str1 − This is the first string to be compared.
str2 − This is the second string to be compared.
Return Value
This function return the integer value −
- If the < 0, str1 is less than str2.
- If the value > 0, str2 is less than str1.
- If the value = 0, str1 is equal to str2.
Example 1
Following is the C library program that illustrates the strcoll() function.
#include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; strcpy(str1, "abc"); strcpy(str2, "ABC"); ret = strcoll(str1, str2); if(ret > 0) { printf("str1 is less than str2"); } else if(ret < 0) { printf("str2 is less than str1"); } else { printf("str1 is equal to str2"); } return(0); }
Output
On execution of above code, we get the following result −
str1 is less than str2
Example 2
Here, we use strcoll() for three times that determines the integer result in positive, negative, and zero.
#include <stdio.h> #include <string.h> int main() { char str1[50] = "abcdef"; char str2[50] = "abcdefgh"; char str3[] = "ghijk"; char str4[] = "GHIJK"; int x, y, z; x = strcoll(str1, str2); printf("\n The Comparison of str1 and str2 Strings = %d", x); y = strcoll(str3, str4); printf("\n The Comparison of str3 and str4 Strings = %d", y); z = strcoll(str1, "abcdef"); printf("\n The Comparison of both Strings = %d", z); return 0; }
Output
The above code produces the following result −
The Comparison of str1 and str2 Strings = -103 The Comparison of str3 and str4 Strings = 32 The Comparison of both Strings = 0
Example 3
Intead of printing the integer values, we print the simple message which shows the comparison statements.
#include <stdio.h> #include <string.h> int main() { char string1[50] = "abcdefgh"; char string2[50] = "ABCDEFGH"; int res res = strcoll(string1, string2); if(res < 0) { printf("\n string1 is Less than string2"); } else if(result > 0) { printf("\n string2 is Less than string1"); } else { printf("\n string1 is Equal to string2"); } return 0; }
Output
After executing the code, we get the following result −
string2 is Less than string1