Reputation: 129
Thanks for the attention.
I write a piece of code to count the number of word, line, and character using C language.
while((c = fgetc(fp)) != EOF)
{
if((char)(c) == ' ' || (char)(c) == '\t'){
num_word++;
num_char++;
}
else if((char)(c) == '\n'){
num_line++;
num_word++;
num_char++;
}
else{
num_char++;
}
}
Everything works fine except for the num_word. For example, if the test case has a blank line, it would count one more.
example for test
case
My program would count 5 instead of 4. Any hints for solving this problem?
Upvotes: 0
Views: 95
Reputation: 957
You are using two branches (space
, \t
and \n
as delimiters) for new words. It fails for continuous whitespace characters.
To solve the problem, you can either skip the continuous whitespace characters (by keeping a counter or flag for it), or set a new non-whitespace character as beginning of a new word.
The C Programming Language by K&R provides very nice examples.
Upvotes: 2
Reputation: 10184
Your word count is 1 more because you have 1 blank line. It will be 2 more if you have 2 blank lines and so on. This is because of the way you are counting the words on line break. The easiest thing you can do is NOT increment the word count if there are successive line breaks indicating blank lines.
char lastChar = ' ';
while((c = fgetc(fp)) != EOF)
{
if((char)(c) == ' ' || (char)(c) == '\t'){
num_word++;
num_char++;
}
else if((char)(c) == '\n'){
num_line++;
if(lastChar != '\n')
num_word++;
num_char++;
}
else{
num_char++;
}
lastChar = (char)c;
}
Upvotes: 1