这是单词计数程序的代码示例。但它不工作。当我们执行它时,在输入单词后,它应该显示结果,但它没有产生任何东西。这段代码中缺少什么吗?
#include<stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* counts lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while( (c = getchar()) != EOF ){
++nc;
if( c == '\n' )
++nl;
if( c == ' ' || c == '\n' || c == '\t' )
state = OUT;
else if( state == OUT ){
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
字符串
2条答案
按热度按时间ajsxfq5m1#
你的代码是好的。你必须问自己如何打破while循环,因为它不断阅读输入,即如何发送
EOF
到你的程序。在 *nix系统上,您可以使用CTRL+D,在Windows上使用CTRL+Z来生成命令。
另外:使用
main()
的标准签名之一,例如int main(void)
。qnyhuwrf2#
这是我的版本这个程序在K&R计数其他字符,以及但我的版本只计数字符,这只是字母表。是的,你必须按Ctrl+d发送信号到这个程序执行printf语句后,而循环。
字符串