STD::C++中的STRNCMP.()

std::strncmp() 函数按字典顺序比较两个以null结尾的字符串中不超过count个字符,并根据结果返回一个整数。

null
  • 此函数包含两个字符串和一个数字 号码 作为论据,最多先进行比较 号码 两个字符串的字节数。
  • 号码 最多应等于最长字符串的长度。如果 号码 定义为大于字符串长度,然后进行比较,直到其中一个字符串的空字符(“”)。
  • 此函数用于按字典顺序比较两个字符串。它从每个字符串的第一个字符开始比较。如果它们相等,则继续并比较每个字符串的下一个字符,依此类推。
  • 此比较过程将停止,直到到达或终止字符串的空字符为止 号码 两个字符串的字符都匹配。

语法:

int strncmp(const char *str1, const char *str2, size_t count);

Parameters:
str1 and str2: C string to be compared.
count: Maximum number of characters to compare.
 size_t is an unsigned integral type.

Return Value: 
Value                               Meaning
Less than zero                      str1 is less than str2.
Zero                                str1 is equal to str2.
Greater than zero                   str1 is greater than str2.

如果两个字符串中的字符数都少于个,则当遇到第一个null时,比较结束。

strcmp()返回什么?

函数返回 三种不同类型的整数值 在比较的基础上:

1.大于零(>0): 如果字符为,则返回正值 str1 str2 在比赛前不匹配 号码 字符和 str1 性格是 更大的 比ASCII值 str2 性格因此,我们可以这样说 str1 在词典编纂上大于 str2 .

// C, C++ program to demonstrate
// functionality of strncmp()
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char str1[10] = "aksh" ;
char str2[10] = "akash" ;
// Compare strings using strncmp()
int result = strncmp (str1, str2, 4);
if (result == 0) {
// num is the 3rd parameter of strncmp() function
printf ( "str1 is equal to str2 upto num characters" );
}
else if (result > 0)
printf ( "str1 is greater than str2" );
else
printf ( "str2 is greater than str1" );
printf ( "Value returned by strncmp() is: %d" , result);
return 0;
}


输出:

str1 is greater than str2
Value returned by strncmp() is: 18

2.小于零(<0): 如果字符为,则返回负值 str1 str2 在比赛前不匹配 号码 字符和 str1 性格是 较小的 比ASCII值 str2 性格因此,我们可以这样说 str2 在词典编纂上大于 str1 .

// C, C++ program to demonstrate
// functionality of strncmp()
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char str1[10] = "akash" ;
char str2[10] = "aksh" ;
// Compare strings using strncmp()
int result = strncmp (str1, str2, 4);
if (result == 0) {
// num is the 3rd parameter of strncmp() function
printf ( "str1 is equal to str2 upto num characters" );
}
else if (result > 0)
printf ( "str1 is greater than str2" );
else
printf ( "str2 is greater than str1" );
printf ( "Value returned by strncmp() is: %d" , result);
return 0;
}


输出:

str2 is greater than str1
Value returned by strncmp() is: -18

3.等于零(0): 如果 str1 str2 直到 号码 角色。因此,我们不能这么说 str1 等于 str2 直到 号码 等于任一字符串的长度。

// C, C++ program to demonstrate
// functionality of strncmp()
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char str1[10] = "akash" ;
char str2[10] = "akas" ;
// Compare strings using strncmp()
int result = strncmp (str1, str2, 4);
if (result == 0) {
// num is the 3rd parameter of strncmp() function
printf ( "str1 is equal to str2 upto num characters" );
}
else if (result > 0)
printf ( "str1 is greater than str2" );
else
printf ( "str2 is greater than str1" );
printf ( "Value returned by strncmp() is: %d" , result);
return 0;
}


输出:

str1 is equal to str2 upto num characters
Value returned by strncmp() is: 0

注: 当字符串不相同时,您会发现strncmp()函数返回的值是str1和str2中第一个不匹配字符的ASCII值之间的差异。

更多例子

例1:

// CPP program to illustrate strncmp()
#include <cstring>
#include <iostream>
void display( char * abc, char * xyz, int res, int count)
{
if (res > 0)
std::cout << xyz << " come-before " << abc;
else if (res < 0)
std::cout << abc << " come-before " << xyz;
else
std::cout << "First " << count << " characters of string "
<< abc << " and " << xyz << " are same" ;
}
int main()
{
char abc[] = "GeeksforGeeks" ;
char xyz[] = "Geeks" ;
int res;
res = std:: strncmp (abc, xyz, 4);
display(abc, xyz, res, 4);
return 0;
}


输出:

First 4 characters of string GeeksforGeeks and Geeks are same

例2:

// CPP program to illustrate strncmp()
#include <cstring>
#include <iostream>
void display( char * abc, char * xyz, int res, int count)
{
if (res > 0)
std::cout << xyz << " come-before " << abc;
else if (res < 0)
std::cout << abc << " come-before " << xyz;
else
std::cout << "First " << count << " characters of string " <<
abc << " and " << xyz << " are same" ;
;
}
int main()
{
char abc[] = "GeeksforGeeks" ;
char xyz[] = "Geeks" ;
int res;
res = std:: strncmp (abc, xyz, 6);
display(abc, xyz, res, 6);
return 0;
}


输出:

Geeks come-before GeeksforGeeks

本文由 阿卡什·古普塔 希瓦尼·古泰尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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