S
shekhardeodhar
The program compiles properly (most of it is from k&r) but the second
function (here character_count) gives wrong answer.
Can someone please explain why ?
#include<stdio.h>
#define IN 1
#define OUT 0
int word_count();
int character_count();
int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());
return 0;
}
int
word_count ()
{
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;
}
}
return nw ;
}
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}
function (here character_count) gives wrong answer.
Can someone please explain why ?
#include<stdio.h>
#define IN 1
#define OUT 0
int word_count();
int character_count();
int
main(void)
{
printf("%d\n",word_count());
printf("%d\n",character_count());
return 0;
}
int
word_count ()
{
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;
}
}
return nw ;
}
int
character_count ()
{
int c;
int nc = 0;
while ((c = getchar ()) != EOF)
++nc;
return nc;
}