程序用于统计行数、单词数与字符数。这里对单词的定义比较宽松,它是任何其中不包含空格,制表符或换行符的字符序列
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc; //characters
if (c == '\n')
++nl; //rows
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw; //words
}
}
printf("%d %d %d\n", nl, nw, nc);
}
转自K&R的《c程序设计语言》
统计行数、单词数与字符数的C程序
本文介绍了一个使用C语言编写的程序,该程序能够统计输入文本的行数、单词数和字符数。程序通过读取字符并判断是否为换行符、空格或制表符来确定行数,同时识别单词序列计算单词数量。
5289

被折叠的 条评论
为什么被折叠?



