The characters may contain any numeric, alphabets, symbols such as :;@ etc. one method is to use a switch case statement as show below. but thats going to be simple and long process. Is there any other method short method possible?
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
fp = fopen("input.txt","r");
int ch,count[36]= {0};
if (fp == NULL)
{
fprintf(stderr,
"Failed to open input.txt: %s\n",
strerror(errno));
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
switch (ch)
{
case 'a':
count[0]++;
break;
case 'b':
count[1]++;
break;
default:
count[2]++;
}
}
fclose(fp);
}
printf("count a is %d", count[0]);
printf("count b is %d", count[1]);
printf("count c is %d", count[2]);
return 0;
}
count[ch]++and you can count ANY bytes in the file.