wcsncmp() function in C/C++
Last Updated :
23 Aug, 2019
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 :
#include <bits/stdc++.h>
using namespace std;
void check( wchar_t * lhs, wchar_t * rhs, int count)
{
int result;
result = wcsncmp(lhs, rhs, count);
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" ;
}
int main()
{
wchar_t lhs[] = L "geekforgeeks" ;
wchar_t rhs[] = L "geekGgeek" ;
check(lhs, rhs, 4);
check(lhs, rhs, 7);
return 0;
}
|
Output:
First 4 characters of are same
geekGgeek precedes geekforgeeks
Program 2 :
#include <bits/stdc++.h>
using namespace std;
void check( wchar_t * lhs, wchar_t * rhs, int count)
{
int result;
result = wcsncmp(lhs, rhs, count);
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" ;
}
int main()
{
wchar_t lhs[] = L "01234GFG" ;
wchar_t rhs[] = L "01234GFG" ;
check(lhs, rhs, 5);
check(lhs, rhs, 8);
return 0;
}
|
Output:
First 5 characters of are same
First 8 characters of are same
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...