Wcscoll Function In C++ - C++ Programming Tutorial
C++ Course / Functions / Wcscoll Function In C++

Wcscoll Function In C++

BLUF: Mastering Wcscoll Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Wcscoll Function In C++

C++ is renowned for its efficiency. Learn how Wcscoll Function In C++ enables low-level control and high-performance computing in the tutorial below.

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:

Example

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++:

Example

#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:

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++:

Example

#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:

Output

In Czech: javacpptutorial precedes JTP
In American English: javacpptutorial precedes JTP

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience