我正在尝试编写一个程序来计算文本中的字符数、单词数和行数,文本为:
It was a dark and stormy night;
the rain fell in torrents - except
at occasional intervals, when it was
checked by a violent gust of wind
which swept up the streets (for it is
in London that our scene lies),
rattling along the housetops, and fiercely
agitating the scanty flame of the lamps
that struggled against the darkness.
Edward Bulwer-Lytton's novel Paul Clifford.
我一直收到 62
而不是 64
,有什么建议吗?
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main() {
int tot_chars = 0; /* total characters */
int tot_lines = 0; /* total lines */
int tot_words = 0; /* total words */
int boolean;
/* EOF == end of file */
int n;
while ((n = getchar()) != EOF) {
tot_chars++;
if (isspace(n) && !isspace(getchar())) {
tot_words++;
}
if (n == '\n') {
tot_lines++;
}
if (n == '-') {
tot_words--;
}
}
printf("Lines, Words, Characters\n");
printf(" %3d %3d %3d\n", tot_lines, tot_words, tot_chars);
// Should be 11 64 375
// rn is 11 65 375
return 0;
}
最佳答案
您的代码中存在多个问题:
- 在测试
if (isspace(n) && !isspace(getchar()))
中,您可能会消耗文件中的一个字节,并且无法增加tot_chars
,而且如果 2 个单词由 2 个空格字符分隔,则不会增加tot_words
。这会导致darkness.
和Edward
被计为单个单词。 当您看到连字符时,您会减少
tot_words
,这是不正确的,因为单词仅由空格分隔。这导致Bulwer-Lytton's
被计为1-1
,即零。因此,您只能得到 62 个单词,而不是 64 个。顺便说一句,名称
n
对于从文件读取的字节来说是令人困惑的。对于伯爵来说,这通常是一个更合适的名字。从文件读取的字节的惯用名称是c
,类型正确为int
,以适应unsigned char
的所有值以及特殊值EOF
。
要检测单词边界,您应该使用状态并在状态更改时更新单词计数:
#include <ctype.h>
#include <stdio.h>
int main(void) {
int tot_chars = 0; /* total characters */
int tot_lines = 0; /* total lines */
int tot_words = 0; /* total words */
int in_space = 1;
int c, last = '\n';
while ((c = getchar()) != EOF) {
last = c;
tot_chars++;
if (isspace(c)) {
in_space = 1;
if (c == '\n') {
tot_lines++;
}
} else {
tot_words += in_space;
in_space = 0;
}
}
if (last != '\n') {
/* count last line if not linefeed terminated */
tot_lines++;
}
printf("Lines, Words, Characters\n");
printf(" %3d %3d %3d\n", tot_lines, tot_words, tot_chars);
return 0;
}
关于C 字数统计程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22969076/