This is a program to print a histogram of the frequencies of different characters in its input. It counts uppercase and lowercase letters together.
The characters should be printed in increasing order of their ASCII value.
#include <string.h>
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[20], new[20];
int i,j,count=0,n;
char temp;
fgets(str, 20, stdin);
//convert higher chars to lower chars
for(i=0;i<strlen(str);i++){
new[i] = tolower(str[i]);
}
//assign n to the length of the string
for(j=0;new[j];j++);
n=j;
//sort the string in ascending order
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (new[i] > new[j]) {
temp = new[i];
new[i] = new[j];
new[j] = temp;
}
}
}
//check and print the count
for(i=0;i<n;i++)
{
count=1;
if(new[i])
{
for(j=i+1;j<n;j++)
{
if(new[i]==new[j])
{
count++;
new[j]='\0'; //make the sec char 0
}
}
printf("%c %d \n",new[i],count);
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire