The user enters a string of characters, but before that he enter the size of the string. Then I have to read and count how many times each letter is entered.
Here is my code:
#include <stdio.h>
char ab[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; //Size = 26
int main(void)
{
int i, j, size, counter[26];
char temp;
for(i=0; i<26; i++)
{
counter[i]=0;
}
scanf("%d",&size);
for(i=0; i<size; i++)
{
scanf("%c",&temp);
for(j=0; j<26; j++)
{
if(ab[j]==temp)
{
counter[j]++;
break;
}
}
}
for(i=0; i<26; i++)
{
printf("We have %d %c\n",counter[i],ab[i]);
}
return 0;
}
And here is my problem:
In the given code the for loop that reads executes one last time. So for example if you enter 7 it will execute 6 times instead of 7 even if it starts from 0. Do you know what the problem is?