//wordcnt.c-単語、単語、行の数を数えます
#include <stdio.h>
#include <ctype.h> // isspace()関数のプロトタイプを提供します
#include <stdbool.h> / / boolの場合、trueとfalseの
定義を指定します#define STOP '|'
int main(void)
{ char c; //文字を読み取るcharprev; //前の文字を読み取るlongn_chars = 0L; //文字数int n_lines = 0; //行数intn_words = 0; //単語数intp_lines = 0; //不完全な行数boolinword = false; // cが単語内にある場合、inwordはtrueに等しい
printf("Enter text to be analyzed (| to terminate):\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("characters = %ld,words = %d,lines = %d,", n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);
return 0;
}