I am trying to count how many times a character appears in a string. I want to count with a for loop for each character that has another loop that iterates through every character of the string. I am not sure what is wrong with it or if it is even possible to do it that way. I have seen other ways it can be done, but I was wondering if I am close to achieving it or my code is entirely faulty.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char A[100], ch;
int cnt[100]={};
gets(A);
int x = strlen(A)-1;
int i=0;
for(ch='a';ch<='z';ch++)
{
for(i = 0; i <= x; i++)
{
if(A[i]==ch)
cnt[i]++;
}
printf("%c is %d times in string\n", ch, cnt[i]);
}
return 0;
}
enter code here