码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C语言错误:计算字符串中特定单词的出现次数


    问题描述:

    这个函数的问题是它会处理所有的子字符串,而不是像这样的词,例如,如果我在“hifive toeveryone”中寻找“hi”,它会返回 1

    1. int HowManyString(char *satz,char *word) {
    2. int coun = 0;
    3. while (strlen(word)<strlen(satz)) {
    4. if (strstr(satz,word)==NULL) {
    5. return coun;
    6. } else {
    7. satz=strstr(satz,word)+strlen(word);
    8. if(*(satz)==' '||*(satz)=='\0'){
    9. coun++;
    10. } else {
    11. return coun;
    12. }
    13. }
    14. }
    15. return coun;
    16. }

    解决思路一:

    这是一个实现您正在寻找的功能:

    1. int count_words(const char *sentence, const char *word)
    2. {
    3. int counter = 0;
    4. for (const char *p = sentence; *p; ++p) {
    5. // Skip whitespaces
    6. if (isspace(*p))
    7. continue;
    8. // Attempt to find a match
    9. const char *wp = word, *sp = p;
    10. while (*wp != '\0' && *sp != '\0' && *wp == *sp) {
    11. ++wp;
    12. ++sp;
    13. }
    14. // Match found AND a space after AND a space before
    15. if (*wp == '\0' && (isspace(*sp) || *sp == '\0') && (p == sentence || isspace(*(p-1)))) {
    16. ++counter;
    17. p = sp - 1;
    18. }
    19. // End of sentence reached: no need to continue.
    20. if (*sp == '\0')
    21. return counter;
    22. }
    23. return counter;
    24. }

    用法:

    1. int main(void)
    2. {
    3. const char sentence[] = "I is Craig whoz not me, not him, not you!";
    4. const char word[] = "not";
    5. int occ = count_words(sentence, word);
    6. printf("There are %d occurences.\n", occ);
    7. }

    输出:

    There are 3 occurences.

    解决思路二(这是解决小编问题的思路):

            以上仅为部分解决思路介绍,请查看全部内容,请添加下方公众号后回复001,即可查看。公众号有许多评分最高的编程书籍和其它实用工具,绝对好用,可以放心使用

            如果您觉得有帮助,可以关注公众号——定期发布有用的资讯和资源​

            

  • 相关阅读:
    vue之使用箭头函数导致表格无法刷新数据
    图书馆公众号自动预约python版
    图片水平垂居中
    零基础Linux_15(基础IO_文件)软硬链接+动静态库详解
    flink1.15.0消费kafka 报错 The coordinator is not available.
    ⚡文件工具类⚡
    STM32 Cube配置RS485 Modbus
    如何防止研发把代码上传到私人gitee/github?
    mysql创建索引导致死锁,数据库崩溃,mysql的表级锁之【元数据锁(meta data lock,MDL)】全解
    电脑上玩GBA游戏(GBA模拟器)
  • 原文地址:https://blog.csdn.net/qq_38334677/article/details/126107213
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号