C++ Reference: Standard C++ Library reference: C Library: cstring: strncmp

C++官网参考链接:https://cplusplus.com/reference/cstring/strncmp/

函数
<cstring>
strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
比较两个字符串的字符
将C字符串str1的最多num个字符与C字符串str2的字符进行比较。
这个函数开始比较每个字符串的第一个字符。如果它们相等,则继续执行以下对,直到字符不同,直到到达一个终止的空字符,或者直到两个字符串中的num个字符匹配,以先发生的情况为准。

形参
str1
要比较的C字符串。
str2
要比较的C字符串。
num 
要比较的最大字符数量。
size_t是无符号整型。

返回值
返回一个整数值,表示字符串之间的关系:

return valueindicates
<0

the first character that does not match has a lower value in str1 than in str2

(第一个不匹配的字符在str1中的值小于str2中的值)

0

the contents of both strings are equal

(两个字符串的内容相等)

>0

the first character that does not match has a greater value in str1 than in str2

(第一个不匹配的字符在str1中的值大于str2中的值)

用例
/* strncmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  int n;
  puts ("Looking for R2 astromech droids...");
  for (n=0 ; n<3 ; n++)
    if (strncmp (str[n],"R2xx",2) == 0)
    {
      printf ("found %s\n",str[n]);
    }
  return 0;

输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值