In this guide, we will explore the functionality of the wcscoll function in C++ along with its syntax and a demonstration.
The C++ wcscoll function is used to compare two null-terminated strings. This function performs the comparison according to the locale set in the LC_COLLATE category.
This function evaluates the first character of each string, treating them as equal until a point of divergence or reaching a null wide character denoting the end of the string.
Syntax:
It has the following syntax:
int wcscoll( const wchar_t* wcs1, const wchar_t* wcs2 )
The function accepts two mandatory parameters, which are described below:
wcs1: It refers to wide strings containing null terminators that require comparison.
The pointer points to the null-terminated wide strings that need to be compared.
Value returned: The function yields the following three values:
- 0 , if the values show that the two strings are identical.
- Positive value: If the first character in WCS1 that differs from WCS2 is more than that character's equivalent.
- Negative Value: If the first character in WCS1 that differs from WCS2 is smaller than the corresponding character in WCS2, the value is negative.
Example 1:
Let's consider an example to demonstrate the application of the wcscoll function in C++:
#include <iostream>
#include <clocale>
#include <cwchar>
void CompareWords(const wchar_t* word1, const wchar_t* word2)
{
if (wcscoll(word1, word2) < 0)
wcout << word1 << L" precedes " << word2 << '\n';
else if (wcscoll(word1, word2) > 0)
wcout << word2 << L" precedes " << word1 << '\n';
else
wcout << word1 << L" equals " << word2 << '\n';
}
int main()
{
// Initializing two words
wchar_t wordA[] = L"apple";
wchar_t wordB[] = L"banana";
// Setting American locale
setlocale(LC_ALL, "en_US.utf8");
wcout << L"In American English: ";
CompareWords(wordA, wordB);
// Setting Swedish locale
setlocale(LC_ALL, "sv_SE.utf8");
wcout << L"In Swedish: ";
CompareWords(wordA, wordB);
return 0;
}
Output:
In American English: apple precedes banana
In Swedish: apple precedes banana
Example 2:
Let's consider another instance to demonstrate the functionality of the wcscoll function in C++:
#include <iostream>
#include <clocale>
#include <cwchar>
void CompareWords(const wchar_t* word1, const wchar_t* word2)
{
if (wcscoll(word1, word2) < 0)
wcout << word1 << L" precedes " << word2 << '\n';
else if (wcscoll(word1, word2) > 0)
wcout << word2 << L" precedes " << word1 << '\n';
else
wcout << word1 << L" equals " << word2 << '\n';
}
int main()
{
// Initializing two words
wchar_t wordJavacpptutorial[] = L"javacpptutorial";
wchar_t wordJTP[] = L"JTP";
// Setting Czech locale
setlocale(LC_COLLATE, "cs_CZ.utf8");
wcout << L"In Czech: ";
CompareWords(wordJavacpptutorial, wordJTP);
// Setting American English locale
setlocale(LC_COLLATE, "en_US.utf8");
wcout << L"In American English: ";
CompareWords(wordJavacpptutorial, wordJTP);
return 0;
}
Output:
In Czech: javacpptutorial precedes JTP
In American English: javacpptutorial precedes JTP