码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 统计单词数量(文件)(*)


    请编写函数,统计英文文章的单词数量。

    函数原型
    int CountWord(FILE *f);

    说明:参数 f 为文件指针。函数值为该文件的单词数量。

    裁判程序
    1. #include
    2. #include
    3. #include
    4. int CountWord(FILE *f);
    5. int main()
    6. {
    7. FILE *f;
    8. int n;
    9. f = fopen("Story.txt", "r");
    10. if (!f)
    11. {
    12. puts("文件无法打开!");
    13. exit(1);
    14. }
    15. n = CountWord(f);
    16. if (fclose(f))
    17. {
    18. puts("文件无法关闭!");
    19. exit(1);
    20. }
    21. printf("单词数: %d\n", n);
    22. return 0;
    23. }
    24. /* 你提交的代码将被嵌在这里 */

    打开 Windows 记事本软件,复制下面的文章内容,保存文件并命名为“Story.txt”。 

    Story.txt

    1. A Cure for a Headache
    2. One day a man went into a chemist's shop and said, "Have you anything to cure a
    3. headache?"
    4. The chemist took a bottle from a shelf, held it under the gentleman's nose and
    5. took out the cork. The smell was so strong that tears came into the man's eyes
    6. and ran down his cheeks.
    7. "What did you do that for?" he said angrily, as soon as he could get back his
    8. breath.
    9. "But that medicine has cured your headache, hasn't it?" said the chemist.
    10. "You fool," said the man, "It's my wife that has the headache, not me!"
    样例输入
    (无)
    
    输出样例
    单词数: 108
    

    代码:

    1. int CountWord(FILE* f)
    2. {
    3. char word[1000];
    4. int count = 0;
    5. while (fscanf(f, "%s", word) != EOF)
    6. {
    7. for (int i = 0; word[i]; i++)
    8. {
    9. if (isalpha(word[i]))
    10. {
    11. count++;
    12. }
    13. }
    14. }
    15. return count;
    16. }

     注意:

    1.fscanf(f, "%s", word) != EOF  每次读取一个字符串,遇到空格不读,地址为word

    2.isalpha为检查所传的字符是否是字母的函数。

  • 相关阅读:
    重装系统会影响到电脑的正常使用吗
    基于Zookeeper手写配置中心
    16-自动化测试——selenium介绍
    Acrel-2000Z电力监控系统在重庆五桂堂历史文化商业街区的应用
    我换了一圈儿,又回来了!
    mysql binlog自动删除与手动删除
    Mysql语句分析、存储引擎、索引优化等详情
    单片机IO口控制12V电压通断,MOS和三极管电路
    Oracle EBS form开发 提示 FRM-15500:Valid and unique object name must be entered
    代码随想录算法训练营第五十七天| LeetCode 392 判断子序列、LeetCode 115 不同的子序列
  • 原文地址:https://blog.csdn.net/Y2663438690/article/details/138085341
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号