C/C中的wcsncmp()函数++

这个 wcsncmp() 函数比较两个宽字符串的字符。比较是按字典顺序进行的。这个函数有三个参数 lhs , rhs 计数 .它比较了 lhs rhs 按字典顺序最多可数个字符。

null

注: 行为 wcsncmp() 未定义,如果 lhs rhs 不要指向以null结尾的宽字符串。

语法:

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

参数: 该函数接受三个强制性参数,如下所述:

  • lhs: 要比较的字符串
  • rhs: 要比较的字符串
  • 计数: 要比较的最大字符数

返回值: 函数返回三个值,如下所示:

  • 正值: 如果lhs中的第一个不同字符大于rhs中的相应字符。
  • 负值: 如果lhs中的第一个不同字符小于rhs中的相应字符。
  • 零: 如果两个字符串中比较的字符构成同一个字符串。

以下程序说明了上述功能: 项目1:

// 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 << "" ;
else if (result > 0)
wcout << rhs << " precedes " << lhs << "" ;
else
wcout << L "First " << count << L " characters of "
<< L " are same"
<< "" ;
}
// Driver code
int main()
{
// initialize 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;
}


输出:

First 4 characters of  are same
geekGgeek precedes geekforgeeks

项目2:

// 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 << "" ;
else if (result > 0)
wcout << rhs << " precedes " << lhs << "" ;
else
wcout << L "First " << count << L " characters of "
<< L " are same"
<< "" ;
}
// Driver code
int main()
{
// initialize 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;
}


输出:

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

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享