首页 > 后端开发 > C++ > 正文

编写一个C程序,使用while循环计算句子的平均单词长度

WBOY
发布: 2023-08-28 18:25:06
转载
1764人浏览过

编写一个c程序,使用while循环计算句子的平均单词长度

问题

在运行时输入一个句子,并编写一段代码来计算句子中出现的单词的平均长度

序列猴子开放平台
序列猴子开放平台

具有长序列、多模态、单模型、大数据等特点的超大规模语言模型

序列猴子开放平台 0
查看详情 序列猴子开放平台

解决方案

算法
START
Step 1: declare character, int and double variables
Step 2: Enter any statement
Step 3: while loop
       Check condition stmt[i]=getchar()) != '</p><p>'
       True then enter into loop
       Increment I and call the function at step 5
Step 4: Print the average length return by function
       From step 5
Step 5: called function calculatewordlength
         i. declare and initialize
            charcount=0 and wordcount=1
         ii. while loop
            check condition (*stmt != '</p><p>')
            if it trues enter into loop
            1.    if(*stmt != ' ')
            2.    charcount++;
            3.    else if(*stmt == ' ')
            4.    wordcount++;
            5.    stmt++;
         iii. return (double)charcount/wordcount;
STOP
登录后复制

程序

#include<stdio.h>
#include<string.h>
double calculatewordlength(const char *stmt);
int main(){
   char stmt[100];
   int i=0;
   double avglen;
   printf("enter any statement:");
   while((stmt[i]=getchar()) != '</p><p>')
      i++;
   stmt[i]='</p><p>';
   avglen=calculatewordlength(stmt);
   printf("average length of word is:%f.</p><p> ", avglen);
}
double calculatewordlength(const char *stmt){
   int charcount=0;
   int wordcount=1;
   while(*stmt != '</p><p>'){
      if(*stmt != ' ')
         charcount++;
      else if(*stmt == ' ')
         wordcount++;
      stmt++;
   }
   return (double)charcount/wordcount;
}
登录后复制

输出

enter any statement:Tutorials Point is the best resource for online education average length of word: 5.444444444.
登录后复制

以上就是编写一个C程序,使用while循环计算句子的平均单词长度的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号