Here is the source code I came up with:
#include <stdio.h>
#define SPACE ' '
int main(void)
{
char c, prev;
int space, newline;
long other;
space = 0;
newline = 0;
other = 0L;
printf("Enter text to be analyzed (# to terminate):\n");
__fpurge(stdin); /* clears buffer */
while((c = getchar()) != '#')
{
if(c == (SPACE || '\n'))
{
if(c == SPACE)
space++;
if(c == '\n')
newline++;
}
else
other++;
}
printf("There are %d spaces, %d newline characters, and %d other characters.\n",
space, newline, other);
getchar();
getchar();
return 0;
}
Most everything works fine, except when I type Brent [ENTER] #
, I get 6 other characters instead of just 5, one for each letter of Brent. I believe it might have something to do with the [ENTER]
but I am not sure. Anyone have any ideas?