C语言实现控制台单词计数比较经典的方法(出自C Programming Language一书的习题)
/*Count the word*/
#define IN 1
#define OUT 2
int main()
{
int c, nw, nl, nc, state;
nl = nc = nw = 0;
state = OUT;
while ((c = getchar()) != EOF')
{
++nc;
if ('\n' == c)
++nl;
if (' '==c || '\t'==c || '\n'==c)
{
state = OUT;
}
else if (OUT == state)
{
state = IN;
++nw;
}
}
#define IN 1
#define OUT 2
int main()
{
int c, nw, nl, nc, state;
nl = nc = nw = 0;
state = OUT;
while ((c = getchar()) != EOF')
{
++nc;
if ('\n' == c)
++nl;
if (' '==c || '\t'==c || '\n'==c)
{
state = OUT;
}
else if (OUT == state)
{
state = IN;
++nw;
}
}
return 0;
}
}
转载于:https://blog.51cto.com/dking94/121427