The wcsncmp
function compares the characters of two wide strings, until a specific number of characters, a null character or a character mismatch. It is defined in the wchar.h
header file.
int wcsncmp(wchar_t* str1, wchar_t* str2, int num);
In the above snippet of code, str1
and str2
are the pointers to wide strings that will be compared. The strings will be compared for the num
number of characters or a null character. If a character mismatch occurs before these, the function stops.
If both the strings are the same till the specified number of characters or a null character (whichever is found first), the function returns 0.
A positive number is returned if the character from str1
is greater than the corresponding character of str2
in case of a mismatch.
A negative number is returned if the character from str1
is lesser than the corresponding character of str2
.
#include<stdio.h>#include<wchar.h>void main() {// declaing and initializing two same wide stringswchar_t str1[] = L"HELLO WORLD";wchar_t str2[] = L"HELLO WORLD";// calling the function and storing the result in a variableint returnedVal = wcsncmp(str1, str2, wcslen(str1));// checking if the strings were found to be same or notif(returnedVal == 0) {printf("Both strings are the same");}else {printf("Strings are not same!");}}
In the above example, we are comparing two same strings. The function will return the value 0.
Note that we have used the
wcslen
function to determine the length ofstr1
to compare both the strings completely. More aboutwcslen
function can be found here.
#include<stdio.h>#include<wchar.h>void main() {// declaing and initializing two same wide stringswchar_t str1[] = L"HELLO WORLD";wchar_t str2[] = L"HELLO";// calling the function and storing the result in a variableint returnedVal1 = wcsncmp(str1, str2, 5);int returnedVal2 = wcsncmp(str1, str2, 8);// checking if the strings were found to be same or notprintf("RESULT FOR COMPARISON TILL 5 CHARACTERS: \n");if(returnedVal1 == 0) {printf("Both strings are the same. \n");}else if(returnedVal1 > 1) {printf("The mismatched character in str1 is greater than the corresponding character in str2 \n");}else if(returnedVal1 < 1) {printf("The mismatched character in str1 is lesser than the corresponding character in str2 \n");}else {printf("Strings are not same!");}printf("\nRESULT FOR COMPARISON TILL 8 CHARACTERS: \n");if(returnedVal2 == 0) {printf("Both strings are the same. \n");}else if(returnedVal2 > 1) {printf("The mismatched character in str1 is greater than the corresponding character in str2 \n");}else if(returnedVal2 < 1) {printf("The mismatched character in str1 is lesser than the corresponding character in str2 \n");}else {printf("Strings are not same!");}}
In the above snippet of code, we are comparingstr1
and str2
till 5 number of characters and then 8 number of characters. It is evident by the output that both the strings are the same for 5 characters (HELLO being compared with HELLO). However, in the case of 8 characters, the mismatch occurs at the 5th index of both strings. The blank space of str1
has a greater value than the null character of `str2.
Free Resources