单词计数 行数计数字符计数

    xiaoxiao2021-10-30  80

    //单词计数 //这里对单词的定义比较宽松,它是任何其中不包括空格,制表符或换行符的字符序列 #include<stdio.h> #include<windows.h> int main() { int c; int nl, nc, nw;//nl行数nc字符数nw单词数 int state; nl = nc = nw = 0; state = 1; while ((c = getchar())!=EOF) { nc++; if (c == '\n') { nl++; } if ( c== '\n' || c == '\t' || c == ' ') { state = 1; } else if (state == 1) { ++nw; state = 0; } } printf("nl:%d\nnw:%d\nnc:%d\n", nl, nw, nc); system("pause"); return 0; }

    提醒: 当统计完一个单词后,标志进行复位。下次遇见单词停顿的地方,标志位置1。

    转载请注明原文地址: https://ju.6miu.com/read-677886.html

    最新回复(0)