C语言中支持单字母输入的Wordcount
Wordcount in C that supports singlar letter input
我在 'wordcount' 正确计数时遇到了一些问题,因为它错过了 'I' 等单数字母。
本质上,如果 space 在 character/symbol 或独立 character/symbol 之间,将计算一个字数。
#include <stdio.h>
int main()
{
int wordcount;
int ch;
char lastch = -1;
wordcount = 0;
while ((ch = getc(stdin)) != EOF) {
if (ch == ' ' || ch == '\n')
{
if (!(lastch == ' ' && ch == ' '))
{
wordcount++;
}
}
lastch = ch;
}
printf("The document contains %d words.", wordcount);
}
您使条件测试过于复杂。如果我理解您的目的,您唯一关心的是 lastch != ' '
和 (ch == ' ' || ch == '\n')
.
此外,getchar
returns 输入 int
。因此,ch
应键入 int
以在所有系统上正确检测 EOF
。
通过这些更改进行简化,您可以执行类似以下操作:
#include <stdio.h>
int main (void) {
int wordcount = 0,
lastch = 0, /* just initialize to zero */
ch; /* ch should be an int */
while ((ch = getc (stdin)) != EOF) {
if (lastch && lastch != ' ' && (ch == ' ' || ch == '\n'))
wordcount++;
lastch = ch;
}
if (lastch != '\n') /* handle no '\n' on final line */
wordcount++;
printf ("The document contains %d %s.\n",
wordcount, wordcount != 1 ? "words" : "word");
return 0;
}
例子Use/Output
$ echo " " | ./bin/wordcnt
The document contains 0 words.
$ echo " t " | ./bin/wordcnt
The document contains 1 word.
$ echo " t t " | ./bin/wordcnt
The document contains 2 words.
注意: 以防止不包含 POSIX eof
的文件的极端情况(例如 '\n'
在文件末尾),您需要添加一个额外的标志,表明至少找到一个字符,并在退出循环后组合检查 lastch
,例如
#include <stdio.h>
int main (void) {
int wordcount = 0,
lastch = 0, /* just initialize to zero */
ch, /* ch should be an int */
c_exist = 0; /* flag at least 1 char found */
while ((ch = getc (stdin)) != EOF) {
if (lastch && lastch != ' ' && (ch == ' ' || ch == '\n'))
wordcount++;
if (ch != ' ' && ch != '\n') /* make sure 1 char found */
c_exist = 1;
lastch = ch;
}
if (c_exist && lastch != '\n') /* handle no '\n' on final line */
wordcount++;
printf ("The document contains %d %s.\n",
wordcount, wordcount != 1 ? "words" : "word");
return 0;
}
极端案例
$ echo -n " t" | ./bin/wordcnt
The document contains 1 word.
我在 'wordcount' 正确计数时遇到了一些问题,因为它错过了 'I' 等单数字母。
本质上,如果 space 在 character/symbol 或独立 character/symbol 之间,将计算一个字数。
#include <stdio.h>
int main()
{
int wordcount;
int ch;
char lastch = -1;
wordcount = 0;
while ((ch = getc(stdin)) != EOF) {
if (ch == ' ' || ch == '\n')
{
if (!(lastch == ' ' && ch == ' '))
{
wordcount++;
}
}
lastch = ch;
}
printf("The document contains %d words.", wordcount);
}
您使条件测试过于复杂。如果我理解您的目的,您唯一关心的是 lastch != ' '
和 (ch == ' ' || ch == '\n')
.
此外,getchar
returns 输入 int
。因此,ch
应键入 int
以在所有系统上正确检测 EOF
。
通过这些更改进行简化,您可以执行类似以下操作:
#include <stdio.h>
int main (void) {
int wordcount = 0,
lastch = 0, /* just initialize to zero */
ch; /* ch should be an int */
while ((ch = getc (stdin)) != EOF) {
if (lastch && lastch != ' ' && (ch == ' ' || ch == '\n'))
wordcount++;
lastch = ch;
}
if (lastch != '\n') /* handle no '\n' on final line */
wordcount++;
printf ("The document contains %d %s.\n",
wordcount, wordcount != 1 ? "words" : "word");
return 0;
}
例子Use/Output
$ echo " " | ./bin/wordcnt
The document contains 0 words.
$ echo " t " | ./bin/wordcnt
The document contains 1 word.
$ echo " t t " | ./bin/wordcnt
The document contains 2 words.
注意: 以防止不包含 POSIX eof
的文件的极端情况(例如 '\n'
在文件末尾),您需要添加一个额外的标志,表明至少找到一个字符,并在退出循环后组合检查 lastch
,例如
#include <stdio.h>
int main (void) {
int wordcount = 0,
lastch = 0, /* just initialize to zero */
ch, /* ch should be an int */
c_exist = 0; /* flag at least 1 char found */
while ((ch = getc (stdin)) != EOF) {
if (lastch && lastch != ' ' && (ch == ' ' || ch == '\n'))
wordcount++;
if (ch != ' ' && ch != '\n') /* make sure 1 char found */
c_exist = 1;
lastch = ch;
}
if (c_exist && lastch != '\n') /* handle no '\n' on final line */
wordcount++;
printf ("The document contains %d %s.\n",
wordcount, wordcount != 1 ? "words" : "word");
return 0;
}
极端案例
$ echo -n " t" | ./bin/wordcnt
The document contains 1 word.