1. ## K&R Exercise 1-14

Hi all,

I hope you dont mind me asking such a simple question but anyway....

I am working through K&R and I am having a bit of a problem with exercise 1-14 in the Arrays section (1.6). I am doing ok I think. Anyhow, for some reason my program seems to be counting the new line character, the spaces and the tab character instead of just skipping them. I think I understand where the problem is (the if statement inside the getchar while loop) but the problem is that I am not sure why that statement is not working in preventing the newlines etc from being counted. Heres what I have so far:

Code:
```#include <stdio.h>

#define NUM_CHARS 256

/* Prints a histogram of the frequencies of different characters in its input */

int main(void)
{
int c, i, j, max;
int charFreq[NUM_CHARS];

max = 0;

for (i = 0; i < NUM_CHARS; ++i)
charFreq[i] = 0;

while ((c = getchar()) != EOF)
{
if (c != ' ' || c != '\n' || c != '\t')
{
++charFreq[c];
if (charFreq[c] >= max)
max = charFreq[c];
}
}

for (j = max; j >= 1; --j)
{
printf(" %d ", j);
for (i = 0; i < NUM_CHARS; ++i)
{
if (charFreq[i] != 0 && j <= charFreq[i])
printf("  x  ");
else if (charFreq[i] != 0 && j > charFreq[i])
printf("     ");
}
printf("\n");
}

printf("   ");

for (i = 0; i < NUM_CHARS; ++i)
{

if (charFreq[i] != 0 && j <= charFreq[i])
{
if (i > 100)
printf(" %d ", i);
if (i < 100 && i >= 10)
printf(" 0%d ", i);
if (i < 10)
printf(" 00%d ", i);

}

}
printf("\n");

return(0);
}```
Please excuse the dodgy histogram display code, I just quickly hacked that in so I could see what was going on but I will go back and tidy it up after I have sorted this problem.

2. Code:
`          if (c != ' ' || c != '\n' || c != '\t')`
Say for example the character you get for c is a space. In that case it goes through and sees that c is equal to a space, but it then sees that it is not equal to a newline or for that matter a tab. Because of this it execute the if statement anyway. To resolve this problem you need to use && instead of ||.
Code:
`          if (c != ' ' && c != '\n' && c != '\t')`

3. This is always true.
Code:
`if (c != ' ' || c != '\n' || c != '\t')`
Perhaps you meant this.
Code:
`if (c != ' ' && c != '\n' && c != '\t')`

4. Ahh, thanks very much guys. Now I see why it didnt work as I originally expected it to. Its funny how you can stare at something for so long and when you find the solution to your problem (or someone else does) you really kick yourself for not spotting it sooner, hehe.

Thanks again