wcscoll() function in C++

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:

  1. wcs1: It is a pointer to the null terminated wide strings to compare.
  2. wcs2: It is a pointer to the null terminated wide strings to compare.

Return value : The function returns three value as below:

  1. 0, if values indicates that both strings are equal.
  2. Positive value, if the first character that is different in wcs1 is greater than the corresponding character in wcs2.
  3. 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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcsoll() function
#include <bits/stdc++.h>
using namespace std;
  
// function to compare two strings
void comparetwo(const wchar_t* wcs1, const wchar_t* wcs2)
{
    // pointer wcs1 points string
    // pointer wcs2 points string_
    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()
{
    // initializing two strings
    wchar_t string[] = L"article";
    wchar_t string_[] = L"everyone";
  
    // setting american locale
    setlocale(LC_ALL, "en_US.utf8");
    wcout << L"In American : ";
  
    // compare two strings with wcs1 and wcs2
    comparetwo(string, string_);
  
    // setting swedish locale
    setlocale(LC_ALL, "sv_SE.utf8");
    wcout << L"In Swedish : ";
    comparetwo(string, string_);
  
    return 0;
}

chevron_right


Output:

In American : article precedes everyone
In Swedish : article precedes everyone

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcsoll() function
#include <bits/stdc++.h>
using namespace std;
  
// function to compare two strings
void comparetwo(const wchar_t* wcs1, const wchar_t* wcs2)
{
    // pointer wcs1 points string
    // pointer wcs2 points string_
    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()
{
    // initializing two strings
    wchar_t string[] = L"geekforgeeks";
    wchar_t string_[] = L"gfg";
  
    // setting Czech locale
    setlocale(LC_COLLATE, "cs_CZ.utf8");
    wcout << L"In Czech : ";
  
    // compare two strings with wcs1 and wcs2
    comparetwo(string, string_);
  
    // setting american locale
    setlocale(LC_COLLATE, "en_US.utf8");
    wcout << "In the American locale: ";
    comparetwo(string, string_);
  
    return 0;
}

chevron_right


Output:

In Czech : geekforgeeks precedes gfg
In the American locale: geekforgeeks precedes gfg


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.