This is my assignment: Write a C program that reads in a string of a defined size from the keyboard and reports how many times each letter of the alphabet occurs within that string (ignoring case). It should also report how many non-letters are included in the string.
So far, I can read in a sentence and it counts the letters of the alphabet. I have copied the code for reading in a string from my lecture notes, but it doesn't seem to work and I'm not sure what I'm doing wrong.
I am also not sure how to count the non-alphabetical characters.
Code:#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); }



LinkBack URL
About LinkBacks


