C 字数统计程序

标签 c word-count

我正在尝试编写一个程序来计算文本中的字符数、单词数和行数,文本为:

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/

相关文章:

javascript - 如何让 Javascript Word Counter 在 IE 中工作

console - openoffice 可以从控制台计算单词吗?

hadoop - 使用 Hadoop MapReduce 对字数进行排序

c - 使用 while 循环检查 struct 的内容

hadoop - 在NetBeans中安装KarmaSphere插件

c - 为客户 C 编程分配点

c - STM32L1x 停止模式 + RTC 电流过大

hadoop - MRv1(mapreduce)和MRv2(YARN)的 “Wordcount”程序是否有所不同

c - 跳跃前先看一下

c - C 或 Obj-C 中的隐写术库