统计录入字符串的字符总数,单词数和行数

来源:互联网 发布:百度人工智能上海招聘 编辑:程序博客网 时间:2024/05/22 07:58

统计录入字符串的字符总数,单词数和行数

包括:

  • 字符读取

  • getchar()函数的使用

  • *循环使用

  • UML序列图和流程图

  • 离线写博客

  • 何时停止读取

  • 一些头文件的使用


#include <stdio.h>//#include <stdlib.h>#include <ctype.h>//为isspace()提供原型#include <stdbool.h>#define STOP '|' //定义结束标志int main(void){   char c;   char prev;//读取的前一个字符   long n_chars=0L;//字符数   int n_lines=0;//行数   int n_words=0;//单词数   int p_lines=0;//不完整的行数   bool inword=false;//字符在单词中,inward等于ture   printf("请输入字符( | 用于结束输入):\n");   prev = '\n';//识别完整的行   while(( c= getchar() )!=STOP)//当读取的字符不为结束字符时   {       n_chars++;//统计字符数       if(c=='\n')       n_lines++;//统计行       if(!isspace(c)&&!inword)       {           inword=true;//开始一个新单词;           n_words++;//统计单词       }       if(isspace(c) && inword)        inword=false;//打到单词的结尾       prev=c;   }   if(prev!='\n')    p_lines=1;   printf("字母数目=%ld,单词数=%d,行数=%d,",n_chars,n_words,n_lines);   printf("不完整的行数为=%d\n",p_lines);   return 0;}

这里写图片描述

总结:

即便是对着书本敲代码,你会发现可能存在各种各样的的问题.

if和while语句括号后面不能加分号………
编译器不会提示错误………..

阅读全文
0 0