C++ cstring strcoll() Function



The C++ strcoll() function is used to compare two strings based on the locale-specific collation order.

The strcoll() function is similar to the strcmp() function but the difference is that strcmp() function compares two strings based on the ASCII value of the characters whereas strcoll() function compares two strings based on the locale-specific collation order.

Let's take an example, where we have a project that shows the regional data. In this project, we need to sort or group the data based on the locale-specific collation order. In this case, we can use the strcoll()

Syntax

Following is the syntax of the strcoll() function.

int strcoll(const char *str1, const char *str2);

Parameters

Parameters of the strcoll() function are as follows −

  • str1: This is the first string to be compared.
  • str2: This is the second string to be compared.

Return Values

This function returns −

  • 0: if both strings are equal.
  • 1: if the first character that does not match has a lower value in str1 than in str2.
  • -1: if the first character that does not match has a greater value in str1 than in str2.

Example 1

This example help you to understand the usage of the C++ strcoll() function. Here, we will take two strings str1 and str2 and then compare them using the strcoll() function.

#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char str1[15];
   char str2[15];
   int ret;

   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcoll(str1, str2);

   if(ret > 0) {
      cout << "str1 is greater than str2"<<endl;
   } else if(ret < 0) {
      cout << "str1 is less than str2"<<endl;
   } else {
      cout << "str1 is equal to str2"<<endl;
   }

   return 0;
}

Output

Following is the output of the above C++ program −

str1 is less than str2

Example 2

Now, Let's take another example where we will compare output of the strcoll() function with the strcmp() function −

#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char str1[15];
   char str2[15];
   int ret;

   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcoll(str1, str2);

   if(ret > 0) {
      cout << "str1 is greater than str2 using strcoll() function"<<endl;
   } else if(ret < 0) {
      cout << "str1 is less than str2 using strcoll() function"<<endl;
   } else {
      cout << "str1 is equal to str2 using strcoll() function"<<endl;
   }

   ret = strcmp(str1, str2);

   if(ret > 0) {
      cout << "str1 is greater than str2 using strcmp() function"<<endl;
   } else if(ret < 0) {
      cout << "str1 is less than str2 using strcmp() function"<<endl;
   } else {
      cout << "str1 is equal to str2 using strcmp() function"<<endl;
   }

   return 0;
}

Output

Following is the output of the above C++ program −

str1 is less than str2 using strcoll() function
str1 is greater than str2 using strcmp() function

You can see that the output of the strcoll() function is different from the output of the strcmp() function. The strcoll() function compares two strings based on the locale-specific collation order whereas the strcmp() function compares two strings based on the ASCII value of the characters.

Advertisements