AppDividend
Latest Code Tutorials

C++ strcoll() Function Example | strcoll() in C++

0

C++ strcoll() is a string comparison function that is defined under the string.h header file. The main advantage of using strcoll() is that it works on the concept of pointers. It has two parameters, which are the strings that are to be compared.

C++ strcoll()

C++ strcoll() compares the string, which is pointed by the first parameter with the one which is pointed by the second one. The main reason why we use strcoll() instead of on strcmp() is that in the case of Unicode characters, the comparison by strcmp() is not accurate. The strcoll() uses the current locale of LC_COLLATE for more accurate results.

Syntax

int strcoll(const char *a, const char *b)

Parameters

Two parameters are passed in the strcoll() method. These two parameters are the strings that are to be compared.

Return Value

It returns an integer value stating different possibilities after comparison.

It returns a value less than zero if the first string is less than the second string.

It returns a value zero if the first string is equal to the second string.

It returns a value greater than 0 if the first string is greater than the second string.

Example programs on strcoll() function

Q1- Write a program to show the mechanism of strcoll() function.

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char a[20] = "Lionel Messi";
  char b[10] = "God";
  int k;
  cout << "a=" << a;
  cout << "\n"
       << "b=" << b << endl;
  k = strcoll(a, b);
  if (k > 0)
  {
    cout << "String a is greater than String b";
  }
  else
  {
    if (k == 0)
    {
      cout << "Both the strings are equal";
    }
    else
    {
      cout << "String a is less than String b";
    }
  }
}

Output

a=Lionel Messi
b=God
String a is greater than String b

Q2- Write a program to compare two equal strings using strcoll().

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char a[20] = "Study";
  char b[10] = "Study";
  int k;
  cout << "a=" << a;
  cout << "\n"
       << "b=" << b << endl;
  k = strcoll(a, b);
  if (k > 0)
  {
    cout << "String a is greater than String b";
  }
  else
  {
    if (k == 0)
    {
      cout << "Both the strings are equal";
    }
    else
    {
      cout << "String a is less than String b";
    }
  }
}

Output

a= Study
b= Study
Both the strings  are equal

Q3 – Write a program to compare two unequal strings using strcoll().

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char a[20] = "Study minds";
	char b[20]= "Study";
	int k;
	cout<<"a="<<a;
	cout<<"\n"<<"b="<<b<<endl;
	k= strcoll(a,b);
	if(k>0)
	{
		cout<<"String a is greater than String b";
	}
	else
	{
		if(k==0)
		{
			cout<<"Both the strings are equal";
		}
		else
		{
			cout<<"String a is less than String b";
		}
	}
}

Output

a= Study minds
b= Study
String a is greater than String b

See also

C++ memset()

C++ strerror()

C++ strlen()

C++ strstr()

C++ strtok()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.