I need to let a user enter an input using getchar, and check for the number of spaces, newlines, and the other characters.
This is my code:
#include <stdio.h>
int main()
{
char word;
int spaces, newLines, theRest;
spaces = 0;
newLines = 0;
theRest = 0;
printf("please type an input:\n");
while ((word = getchar() != '#'))
{
if (word == ' ')
spaces++;
else if (word == '\n')
newLines++;
else
theRest++;
}
printf("number of spaces: %d, number of new lines: %d, other characters: %d", spaces, newLines, theRest);
}
For any input i provide, im getting only theRest value, which includes all the characters.
Could you please tell me what am i doing wrong here?
should be: