文章

36

粉丝

0

获赞

146

访问

33.5k

头像
统计单词 题解:c 注意分类讨论
P1394 华中科技大学
发布于2024年3月6日 13:42
阅读数 495

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. #include <string.h>
  5. int main(void) {
  6. char s[1000];
  7. int count;
  8. while (gets(s)!=NULL) {
  9. for(int i=0;i<strlen(s);i++){
  10. if(s[i]=='.'&&(s[i-1]>='a'&&s[i-1]<='z')){ //如果识别到‘.’并且‘.’前一位是字母才会打印 推出循环
  11. printf("%d ",count);
  12. break;
  13. }
  14. else if(s[i]!=' '){ //如果识别字符不是空格
  15. if(s[i-1]==' '||i==0) count = 1; //如果前一位是空格或者是第一个单词,则开始计数
  16. else if(s[i-1]!=' ' && i!=0) count++; //如果前一位不是空格,就继续计数
  17. }
  18. else if(s[i]==' '&&(s[i-1]>='a'&&s[i-1]<='z')) printf("%d ",count); //如果是单词后第一个空格,输出
  19. }
  20. printf("\n");
  21. }
  22. return 0;
  23. }

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发