I'm working on a program that can measure the frequency of letters that appear in a string given by the user. What makes this program a little different is that the frequency I'm trying to obtain for each letter is the evaluation of the amount of times a character appears divided by the total length of the string itself. I'm nearly done with the program but I keep getting all zeroes for all the letters of the alphabet no matter what I do. How can I fix this? This is my program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXCHAR 1000
int main()
{
int c = 0, count[26] = {0}, x, length = 0;
double frequency;
char str[MAXCHAR];
printf("Enter a string\n");
gets(str);
while (str[length] != '\0')
length++;
while (str[c] != '\0')
{
/** Considering characters from 'a' to 'z' only and ignoring others. */
if (str[c] >= 'a' && str[c] <= 'z')
{
x = str[c] - 'a';
count[x]++;
}
c++;
}
printf("%d \n" , length);
frequency = (count[c]/length);
for (c = 0; c < 26; c++)
printf("%c %lf \n", c + 'a', frequency);
return 0;
}