wcsncmp() function in C/C++

The wcsncmp() function in C/C++ compare characters of two wide strings. The comparison is done lexicographically. This function takes three arguments lhs, rhs and count. It compares the contents of lhs and rhs lexicographically upto a maximum of count wide characters.

Note: The behaviour of wcsncmp() is undefined if either of lhs or rhs do not point to null terminated wide strings.

Syntax:

int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, size_t count )

Parameter :The function accepts three mandatory parameters which are described below:

  • lhs : String to be compared
  • rhs : String to be compared
  • count : Maximum number of characters to compare

Return value: The function returns three value as below:

  • Positive value: if the first differing character in lhs is greater than the corresponding character in rhs.
  • Negative value: if the first differing character in lhs is less than the corresponding character in rhs.
  • Zero: if the characters compared in both the strings form the same string.

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// wcsncmp() function
#include <bits/stdc++.h>
using namespace std;
  
// function to compare two strings
void check(wchar_t* lhs, wchar_t* rhs, int count)
{
    int result;
    // compare lhs and rhs by wcsncmp
    result = wcsncmp(lhs, rhs, count);
  
    // print, as per result
    if (result < 0)
        wcout << lhs << " precedes " << rhs << "\n";
    else if (result > 0)
        wcout << rhs << " precedes " << lhs << "\n";
    else
        wcout << L"First " << count << L" characters of "
              << L" are same"
              << "\n";
}
  
// Driver code
int main()
{
    // intialize two strings lhs and rhs to compare
    wchar_t lhs[] = L"geekforgeeks";
    wchar_t rhs[] = L"geekGgeek";
  
    // check till 4th characters and till 7th characters
    check(lhs, rhs, 4);
    check(lhs, rhs, 7);
  
    return 0;
}

chevron_right


Output:

First 4 characters of  are same
geekGgeek precedes geekforgeeks

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// wcsncmp() function
#include <bits/stdc++.h>
using namespace std;
  
// function to compare two strings
void check(wchar_t* lhs, wchar_t* rhs, int count)
{
    int result;
    // compare lhs and rhs by wcsncmp
    result = wcsncmp(lhs, rhs, count);
  
    // print, as per result
    if (result < 0)
        wcout << lhs << " precedes " << rhs << "\n";
    else if (result > 0)
        wcout << rhs << " precedes " << lhs << "\n";
    else
        wcout << L"First " << count << L" characters of "
              << L" are same"
              << "\n";
}
  
// Driver code
int main()
{
    // intialize two strings lhs and rhs to compare
    wchar_t lhs[] = L"01234GFG";
    wchar_t rhs[] = L"01234GFG";
  
    // check till 5th character
    // and again till 8th character
    check(lhs, rhs, 5);
    check(lhs, rhs, 8);
  
    return 0;
}

chevron_right


Output:

First 5 characters of  are same
First 8 characters of  are same


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.