I'm trying to write a program which reads in a string of defined size from the keyboard and displays how many times each letter appears and also displays how many non-alphabetical characters appear.
So far, it successfully counts the number of letters. However, I'm really struggling with getting the string to work and I'm also unsure of how to count the non-alphabetical characters.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int Pos, CountArray[26];
char ch;
const int MAXSIZE = 10;
int main ()
{
char line[MAXSIZE] = {'\0'};
printf("Enter string: ");
fgets(line, sizeof(line), stdin);
line[strlen(line) - 1] = '\0';
for(Pos=0;Pos<26;Pos++)
{
CountArray[Pos] = 0;
}
printf("Enter a sentence, ending with a fullstop: ");
ch=getchar();
while(ch != '.')
{
if(isalpha(ch))
{
ch = toupper(ch);
CountArray[ch-'A']++;
}
ch = getchar ();
}
for(Pos=0;Pos<26;Pos++)
{
printf("%c=", Pos+'A');
printf("%d: ", CountArray[Pos]);
}
return(0);
}