一个统计单词的程序,编译通过,运行不对。
如题,下面是我的代码:
程序代码:
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#define STOP '|'
int main()
{
char c;
char prev;
long n_chars = 0L; //字符数
int n_lines = 0; //行数
int n_words = 0; //单词数
int p_lines = 0; //不完整的行数
bool inword = false; //如果c在单词中,inword等于true
printf("Enter text to be analyzed(| to terminare):\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;
}
运行结果:







