wcscoll() function in C++
Last Updated :
14 Sep, 2018
wcscoll() function in C++ compares two null-terminated string. The comparison is based on the current locale defined by the LC_COLLATE category.
This function starts comparing the first character of each string. They are considered as equal until the characters differ or until a null wide character signaling the end of a string is reached.
Syntax:
int wcscoll( const wchar_t* wcs1, const wchar_t* wcs2 )
Parameters: The function accepts two mandatory parameters which are described below:
- wcs1: It is a pointer to the null terminated wide strings to compare.
- wcs2: It is a pointer to the null terminated wide strings to compare.
Return value : The function returns three value as below:
- 0, if values indicates that both strings are equal.
- Positive value, if the first character that is different in wcs1 is greater than the corresponding character in wcs2.
- Negative value, if the first character that is different in wcs1 is less than the corresponding character in wcs2.
Below programs illustrate the above function:
Program 1 :
#include <bits/stdc++.h>
using namespace std;
void comparetwo( const wchar_t * wcs1, const wchar_t * wcs2)
{
if (wcscoll(wcs1, wcs2) < 0)
wcout << wcs1 << L " precedes " << wcs2 << '\n' ;
else if (wcscoll(wcs1, wcs2) > 0)
wcout << wcs2 << L " precedes " << wcs1 << '\n' ;
else
wcout << wcs2 << L " equals " << wcs1 << '\n' ;
}
int main()
{
wchar_t string[] = L "article" ;
wchar_t string_[] = L "everyone" ;
setlocale (LC_ALL, "en_US.utf8" );
wcout << L "In American : " ;
comparetwo(string, string_);
setlocale (LC_ALL, "sv_SE.utf8" );
wcout << L "In Swedish : " ;
comparetwo(string, string_);
return 0;
}
|
Output:
In American : article precedes everyone
In Swedish : article precedes everyone
Program 2 :
#include <bits/stdc++.h>
using namespace std;
void comparetwo( const wchar_t * wcs1, const wchar_t * wcs2)
{
if (wcscoll(wcs1, wcs2) < 0)
wcout << wcs1 << L " precedes " << wcs2 << '\n' ;
else if (wcscoll(wcs1, wcs2) > 0)
wcout << wcs2 << L " precedes " << wcs1 << '\n' ;
else
wcout << wcs2 << L " equals " << wcs1 << '\n' ;
}
int main()
{
wchar_t string[] = L "geekforgeeks" ;
wchar_t string_[] = L "gfg" ;
setlocale (LC_COLLATE, "cs_CZ.utf8" );
wcout << L "In Czech : " ;
comparetwo(string, string_);
setlocale (LC_COLLATE, "en_US.utf8" );
wcout << "In the American locale: " ;
comparetwo(string, string_);
return 0;
}
|
Output:
In Czech : geekforgeeks precedes gfg
In the American locale: geekforgeeks precedes gfg
Share your thoughts in the comments
Please Login to comment...