m0_59450311 2021-06-18 17:38 采纳率: 100%
浏览 148
已采纳

c语言的子串出现次数

题目10;子串出现次数
八个英文长句,事先存放于字符数组中。输入一个单词判断该单词在长句中出现了几次。区分大小写。
如: In the _end, it's_ not the _years in your. life _that _count. (表示空格输入: the, 输出: the 在本句出现了2次。
输入: THE, 输出: THE 在本句出现了0次。

 

  • 写回答

4条回答 默认 最新

  • 自信男孩 2021-06-18 17:48
    关注
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        const char *str = ": In the _end, it's_ not the _years in your. life _that _count.", *pstr;
        char substr[32];
        int cnt = 0;
    
        scanf("%s", substr);
        //len = strlen(substr);
        pstr = strstr(str, substr);
        while (pstr) {
            cnt++;
            pstr = strstr(pstr+1, substr);
        }
    
        printf("cnt = %d\n", cnt);
        return 0;
    }

    供参考~

    尝试使用strstr函数~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度