wmemcmp() function in C/C++
Last Updated :
05 Dec, 2019
The wmemcmp() function in C/C++ compare two wide characters. This function compares the first num wide characters of two wide characters pointed by str1 and str2, returns zero if both the strings are equal or different value from zero if they are not.
Syntax:
int wmemcmp (const wchar_t* str1, const wchar_t* str2, size_t num);
Parameters:
- str1: specifies the pointer to the first string.
- str2: specifies the pointer to the second string.
- num: specifies the number of character to be compared.
Return value: This function returns three different values which defines the relationship between two strings:
- Zero: when both strings are equal.
- Positive value: when the first wide character that does not match in both strings has a greater frequency in str1 than in str2.
- Negative value: when first wide character that does not match in both strings has less frequency in str1 than in str2
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
wchar_t str1[] = L "geekforgeeks" ;
;
wchar_t str2[] = L "geekforgeeks" ;
int print = wmemcmp(str1, str2, 12);
wprintf(L "wmemcmp comparison: %ls\n" ,
print ? L "not equal" : L "equal" );
return 0;
}
|
Output:
wmemcmp comparison: equal
Program 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
wchar_t str1[] = L "geekforgeeks" ;
;
wchar_t str2[] = L "geekforgeeks" ;
int first = wcsncmp(str1, str2, 20);
int second = wmemcmp(str1, str2, 20);
wprintf(L "wcsncmp comparison: %ls\n" ,
first ? L "not equal" : L "equal" );
wprintf(L "wmemcmp comparison: %ls\n" ,
second ? L "not equal" : L "equal" );
return 0;
}
|
Output:
wcsncmp comparison: equal
wmemcmp comparison: not equal
Share your thoughts in the comments
Please Login to comment...