How to write a C Program String - Alphabet Count Example in C Programming Language ?
Solution For C Program :
//C Program String - Alphabet Count Example.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
char s[1005];
gets(s);
// All transfer lowercase
int i;
int length = strlen(s);
for( i = 0 ; i < length ; i++ ){
s[i] = tolower(s[i]);
}
// There are several computing a ~ z
//int countA = 0, countB = 0, countC = 0; // ....
int count[26] = {0};
for( i = 0 ; i < length ; i++ ){
/*
if( s[i] == 'a' ){
count[0]++;
}
else if( s[i] == 'b' ){
count[1]++;
}
// .....
*/
if( islower(s[i]) ){
//count[s[i]-97]++;
int index = s[i] - 'a';
count[index]++;
}
}
for( i = 'a' ; i <= 'z' ; i++ ){
int index = i - 'a';
printf("%c: %d\n", i, count[index]);
}
return 0;
}