So I have to create a program that reads the user input and shows how many times each letter appears in that string, and also how many non-letters but my code for alphabets is showing random numbers..
#include <stdio.h>
#include <string.h>
#define SIZE 100
void readInput (char string[]);
void Calc(char string[], int letters[]);
void
readInput (char string[])
{
printf ("Enter a String:\n");
fgets (string, SIZE, stdin);
string[strlen (string) - 1] = '\0';
}
void
Calc(char string[], int letters[])
{
int c = 0, x;
while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
{
x = string[c] - 'a';
letters[x]++;
}
c++;
}
for (c = 0; c < 26; c++)
printf ("%c occurs %d times in the entered string.\n", c + 'a', letters[c]);
}
int
main ()
{
char string[SIZE];
int letters[26];
readInput (string);
Calc(string, letters);
return 0;
}
I'm new to strings I've googled examples but can't seem to find whats wrong with my code and no idea how I will include the non-letters part.