strnstr(char* s1, char*s2, int pos1) 源码

 大家一看就知道这个是什么作用,这个函数呢,大部分OS中都没有实现

strnstr函数说明,在s1的前pos1个字符串中查找s2,如果存在就返回s2在s1中的位置,如果没有找到,返回 NULL,经测试没有问题

以下是源码

/**
 * strnstr - Find the first substring in a %NUL terminated string
 * @s1: The string to be searched
 * @s2: The string to search for
 * @pos1: 在s1的前pos1字符中查找
 * add by zoukaiping*/
char* strnstr(char* s1, char* s2, int pos1)
{
 int l1, l2;

 l2 = strlen(s2);
 if (!l2)
  return (char *)s1;
 l1 = strlen(s1);
 pos1 = (pos1 > s1)?s1:pos1;
 while (pos1 >= l2) {
  pos1--;
  if (!memcmp(s1, s2, l2))
   return (char *)s1;
  s1++;
 }
 return NULL;
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值