I was trying to write a program that counts each occurrence of a character in a given array of characters...
Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int c;
int counter[256 + 1] = {0};
printf("Type your name followed by #: ");
while((c = getchar()) != '#')
{
if(c == ' ' || c == '\n' || c == '\t')
continue;
++counter[tolower(c)];
}
for(c = 0; c < 256; c++) {
if(counter[c] > 0)
printf("%c = %d\n", counter[c], counter[c]);
}
printf("\n");
return 0;
}
It seems to be counting the characters correctly but take a look at what it displays when I type, let's say, Max Payne:
Type your name followed by #: Max Payne#
☻ = 2
☺ = 1
☺ = 1
☺ = 1
☺ = 1
☺ = 1
☺ = 1
Can someone help me fix this? Thanks.