c语言 字符串 查找指定 子字符串 的个数

一、测试思路

  使用C语言来查找一个字符串中指定数量的子字符串,使用 strncmp 函数或者 memcmp 函数,遍历主字符串并计数子字符串出现的次数。或者使用 strstr 函数strstr 函数是 C 语言标准库 <string.h> 中的一个函数,它用于在一个字符串中查找另一个字符串首次出现的位置。虽然 strstr 本身并不直接返回子字符串出现的次数,但可以使用它作为基础来编写一个函数来计算子字符串出现的次数。

二、方式1

  测试代码,程序定义了一个countSubstring函数,它接受两个参数:主字符串和子字符串。然后,它遍历主字符串,并使用strncmp函数来检查当前位置开始的子串是否与给定的子字符串匹配。如果匹配,则计数器增加。最后,它返回计数器的值。代码如下:

#include <stdio.h>  
#include <string.h>  
  
// 函数声明  
int countSubstring(const char *mainStr, const char *subStr);  
  
int main(void) 
{  
    const char *mainStr = "Hello, hello world! Hello everyone.";  
    const char *subStr = "Hello";  
  
    int count = countSubstring(mainStr, subStr);  
    printf("Substring %s appears %d times in %s.\n", subStr, count, mainStr);  
  
    return 0;  
}  
  
// 函数定义:计算子字符串在主字符串中出现的次数  
int countSubstring(const char *mainStr, const char *subStr) 
{  
    int mainStrLen = strlen(mainStr);  
    int subStrLen = strlen(subStr);  
    int count = 0; 
    int mainLen= mainStrLen-subStrLen;
    for (int i = 0; i <= mainLen; i++) 
    {  
        if (strncmp(&mainStr[i], subStr, subStrLen) == 0) 
        { 
	    printf(" mainStr %s \r\n i=%d\n",  &mainStr[i],i);  
            count++;  
        }  
    }  
    return count;  
}

测试结果:
在这里插入图片描述
或者 memcmp 函数

#include <stdio.h>  
#include <string.h>  
  
// 函数声明  
int countSubstring(const char *mainStr, const char *subStr);  
  
int main(void) 
{  
    const char *mainStr = "Hello, hello world! Hello everyone. Hello Hello";  
    const char *subStr = "Hello";  
  
    int count = countSubstring(mainStr, subStr);  
    printf("Substring %s appears %d times in %s.\n", subStr, count, mainStr);  
  
    return 0;  
}  
  
// 函数定义:计算子字符串在主字符串中出现的次数  
int countSubstring(const char *mainStr, const char *subStr) 
{  
    int mainStrLen = strlen(mainStr);  
    int subStrLen = strlen(subStr);  
    int count = 0; 
    int mainLen= mainStrLen-subStrLen;
    for (int i = 0; i <= mainLen; i++) 
    {  
        if (memcmp(&mainStr[i], subStr, subStrLen) == 0) 
        { 
	    printf(" mainStr %s \r\n i=%d\n",  &mainStr[i],i);  
            count++;  
        }  
    }  
    return count;  
}

三、方式2

  测试代码,countSubstringWithStrstr 函数使用了一个 while 循环和 strstr 函数来查找子字符串在主字符串中的位置。每次找到子字符串后,计数器 count 增加,并将 ptr 指针移动到子字符串之后的位置,以便在下一次迭代中继续查找。当 strstr 返回 NULL 时,表示已经查找完整个主字符串,没有找到更多的子字符串,循环结束。最后,函数返回计数器的值,即子字符串在主字符串中出现的次数。代码如下:

#include <stdio.h>  
#include <string.h>  
  
// 函数声明  
int countSubstringWithStrstr(const char *mainStr, const char *subStr);  
  
int main(void) 
{  
    const char *mainStr = "Hello, hello world! Hello everyone.";  
    const char *subStr = "Hello";  
  
    int count = countSubstringWithStrstr(mainStr, subStr);  
    printf("Substring %s appears %d times in %s.\n", subStr, count, mainStr);  
  
    return 0;  
}  
  
// 函数定义:使用strstr计算子字符串在主字符串中出现的次数  
int countSubstringWithStrstr(const char *mainStr, const char *subStr) 
{  
    int count = 0;  
    const char *ptr = mainStr;  
    while ((ptr = strstr(ptr, subStr)) != NULL) 
    {
        printf(" mainStr %s \r\n i=%d\n",  ptr,ptr-mainStr);    
        count++;  
        ptr += strlen(subStr); // 移动到子字符串之后的位置继续查找  
    }  
    return count;  
}

测试结果:
在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值