This problem is from K&R p. 20: Write a program to count blanks, tabs, and newlines.
Here's my attempt:
#include <stdio.h>
int main()
{
int character, whitespace = 0;
printf("Enter some text, and press Ctrl-d when you're done.\n\n");
while((character = getchar() != EOF) {
if(character == (' ' || '\n' || '\t')) {
++whitespace;
}
}
printf("\nYour text contains %d spaces, tabs, and lines.\n", whitespace);
return 0;
}
The program doesn't work. It always gives the answer 0 no matter how many spaces, tabs, and newlines the user text contains. Can anyone see the problem? There's one other strange thing: I have to press Ctrl-d twice for it to register. I have no idea why. Thanks!
isspace
will be available as a macro or function depending on your system and saves you having to second guess what might constitute whitespace in your environment. Strictly speaking, it may be all of the following characters on your system. GNU C certainly thinks so.This is how you can do the test.
the problem with your code is
if(character == (' ' || '\n' || '\t'))
statement. The statement(' ' || '\n' || '\t')
is equivalent to32 || 13 || 9
(each character replaced by it equivalent ASCII value) which is equal to1
as any not zero thing is consider astrue
in C/C++, so effectively you are doingif(character == 1)
. Now I think you can fix the problem in your code.Also books says to count blanks, tabs, and newlines separately and you are trying to count the total numbers, so do something like this.
If you want a complete solution, here is one which i had written a long time back.
The examples above are technically correct. It prints the values only after an
EOF
(End Of File) indicator is called. However, I think there is a better explanation of this exercise (1.8), so let me purpose an alternative. The code below will print the new lines, tabs and blanks right after each new line.tests whether
character
is equal to the result of(' ' || '\n' || '\t')
(the result of this is 1, indicating that the result of the||
is true). You need to test it individually against each of the three possible values, e.g.,One of the issues you might be hitting is your condition.
Try something like:
Parenthesis in your while statement is wrong, it should be
You assigned to character the value of the test
getchar() != EOF
which is 1 for whatever character was really read.