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.