单词计数以及1.12
统计行数,单词数与字符数
1 #include <stdio.h> 2 3 #define IN 1 4 #define OUT 0 5 6 int main() 7 { 8 int c, nl, nc, nw, n; 9 n = nc = nw = 0; 10 nl = OUT; 11 while((c = getchar()) != EOF){ 12 n++; //统计字符数 13 if(c == '\n') 14 nc++; //统计行数 15 if(c == ' ' || c == '\n' || c == '\t') 16 nl = OUT; //单词外nl记为OUT 0 17 else if(nl == OUT){ //输入到单词首字母时执行,nl复制IN 1,并统计单词数 18 nl = IN; 19 nw++; 20 } 21 } 22 printf("字符数%d 行数%d 单词数%d", n, nc, nw); 23 return 0; 24 }
1.12 编写一个程序,以每行一个单词的形式打印其输入
#include<stdio.h> #define OUT 0 #define IN 1 int main(){ int c, nl; nl = OUT; while((c = getchar()) != EOF){ if(c == ' ' || c == '\n' || c == '\t'){ if(nl == IN){ putchar('\n'); nl = OUT; } } else if(nl == OUT){ nl = IN; putchar(c); } else putchar(c); } return 0; }