不区分大小写的strstr

本文介绍了一种不区分大小写的子串查找方法,并提供了具体的C语言实现代码。该方法适用于需要进行大小写不敏感匹配的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、后来发现可以直接使用

单字符 用StrStrI 
宽字符 用StrStrIW

2、这个是从网上找的,不区分大小写的子串查找

char* CP_stristr(const char *pcString1, const char *pcString2)
{
 char *pCompareStart = (char *)pcString1;
 char *pCursor_S1, *pCursor_S2;
 char cSrc, cDst;
 
 // If there is a null source string - this is a "no match"
 
 if (!pcString1)
  return NULL;
  
 // Null length string 2 - this is a "no match"
 if (!*pcString2)
  return NULL;
  
 // Search from every start pos in the source string
 while (*pCompareStart)
 {
  pCursor_S1 = pCompareStart;
  pCursor_S2 = (char *)pcString2;
  
  // Scan both string
  
  while (*pCursor_S1 && *pCursor_S2)
  {
   cSrc = *pCursor_S1;
   cDst = *pCursor_S2;
   
   // Correct case
   
   if ((cSrc >= 'A') && (cSrc <= 'Z'))
    cSrc -= ('A' - 'a');
    
   if ((cDst >= 'A') && (cDst <= 'Z'))
    cDst -= ('A' - 'a');
    
   if (cSrc != cDst)
    break;
    
   pCursor_S1++;
   pCursor_S2++;
  }
  
  // If string 2 is exhausted - there is a match
  
  if (!*pCursor_S2)
   return(pCompareStart);
   
  // Offset source and continue
  pCompareStart++;
 }
 
 return NULL;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值