为什么我的 wc 实现给出了错误的字数?
Why is my wc implementation giving wrong word count?
这是一小段代码。
while((c = fgetc(fp)) != -1)
{
cCount++; // character count
if(c == '\n') lCount++; // line count
else
{
if(c == ' ' && prevC != ' ') wCount++; // word count
}
prevC = c; // previous character equals current character. Think of it as memory.
}
现在,当我 运行 wc
使用包含上述片段代码的文件(按原样)时,我得到 48 个单词,但是当我对相同的输入数据使用我的程序时,我是得到59个字。
如何像 wc 一样计算字数?
与其只检查 space,不如检查 \t \n space 等转义序列。
这将给出正确的结果。
您可以使用来自 <ctype.h>
的 isspace()
换行
if(c == ' ' && prevC != ' ') wCount++;
到
if(isspace(c) && !(isspace(prevC)) wCount++;
这会给出正确的结果。
不要忘记包括 <ctype.h>
您将任何不是 space 的词都视为有效词。这意味着换行符后跟 space 是一个单词,并且由于您的输入(即您的代码片段)是缩进的,所以您会得到一堆额外的单词。
您应该使用 isspace
检查白色 space 而不是将字符与 ' '
:
进行比较
while((c = fgetc(fp)) != EOF)
{
cCount++;
if (c == '\n')
lCount++;
if (isspace(c) && !isspace(prevC))
wCount++;
prevC = c;
}
书中有你想要的功能的例子:"Brian W Kernighan And Dennis M Ritchie: The Ansi C Programming Language"。正如作者所说:这是 UNIX 程序 wc 的基本版本。更改为只计算单词是这样的:
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* nw counts words in input */
main()
{
int c, nw, state;
state = OUT;
nw = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d\n", nw);
}
你可以这样做:
int count()
{
unsigned int cCount = 0, wCount = 0, lCount = 0;
int incr_word_count = 0;
char c;
FILE *fp = fopen ("text", "r");
if (fp == NULL)
{
printf ("Failed to open file\n");
return -1;
}
while((c = fgetc(fp)) != EOF)
{
cCount++; // character count
if(c == '\n') lCount++; // line count
if (c == ' ' || c == '\n' || c == '\t')
incr_word_count = 0;
else if (incr_word_count == 0) {
incr_word_count = 1;
wCount++; // word count
}
}
fclose (fp);
printf ("line : %u\n", lCount);
printf ("word : %u\n", wCount);
printf ("char : %u\n", cCount);
return 0;
}
这是一小段代码。
while((c = fgetc(fp)) != -1)
{
cCount++; // character count
if(c == '\n') lCount++; // line count
else
{
if(c == ' ' && prevC != ' ') wCount++; // word count
}
prevC = c; // previous character equals current character. Think of it as memory.
}
现在,当我 运行 wc
使用包含上述片段代码的文件(按原样)时,我得到 48 个单词,但是当我对相同的输入数据使用我的程序时,我是得到59个字。
如何像 wc 一样计算字数?
与其只检查 space,不如检查 \t \n space 等转义序列。
这将给出正确的结果。
您可以使用来自 <ctype.h>
换行
if(c == ' ' && prevC != ' ') wCount++;
到
if(isspace(c) && !(isspace(prevC)) wCount++;
这会给出正确的结果。
不要忘记包括 <ctype.h>
您将任何不是 space 的词都视为有效词。这意味着换行符后跟 space 是一个单词,并且由于您的输入(即您的代码片段)是缩进的,所以您会得到一堆额外的单词。
您应该使用 isspace
检查白色 space 而不是将字符与 ' '
:
while((c = fgetc(fp)) != EOF)
{
cCount++;
if (c == '\n')
lCount++;
if (isspace(c) && !isspace(prevC))
wCount++;
prevC = c;
}
书中有你想要的功能的例子:"Brian W Kernighan And Dennis M Ritchie: The Ansi C Programming Language"。正如作者所说:这是 UNIX 程序 wc 的基本版本。更改为只计算单词是这样的:
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* nw counts words in input */
main()
{
int c, nw, state;
state = OUT;
nw = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d\n", nw);
}
你可以这样做:
int count()
{
unsigned int cCount = 0, wCount = 0, lCount = 0;
int incr_word_count = 0;
char c;
FILE *fp = fopen ("text", "r");
if (fp == NULL)
{
printf ("Failed to open file\n");
return -1;
}
while((c = fgetc(fp)) != EOF)
{
cCount++; // character count
if(c == '\n') lCount++; // line count
if (c == ' ' || c == '\n' || c == '\t')
incr_word_count = 0;
else if (incr_word_count == 0) {
incr_word_count = 1;
wCount++; // word count
}
}
fclose (fp);
printf ("line : %u\n", lCount);
printf ("word : %u\n", wCount);
printf ("char : %u\n", cCount);
return 0;
}