C语言的写一个小程序,查看一句话中相同字母的个数,谢谢了

刚入门C语言,想请教下大家,谢谢了
编写一个程序,要求用户输入以句号结尾的句子,并打印出每个字符在句子中出现的次数,仅限于出现至少一次的字符。一个字母的大小写版本应该算在同一个字母上。

程序的功能需要达到下面的效果(下面冒号后面的句子由用户输入):
Enter a sentence (end by ‘.’): This is a short sentence.
Occurrences of ‘a’: 1
Occurrences of ‘c’: 1
Occurrences of ‘e’: 3
Occurrences of ‘h’: 2
Occurrences of ‘i’: 2
Occurrences of ‘n’: 2
Occurrences of ‘o’: 1
Occurrences of ‘r’: 1
Occurrences of ‘s’: 4
Occurrences of ‘t’: 3

第1个回答  2019-02-25

#include<stdio.h>
#include<ctype.h>
int main()
{ int i,a[26]={0};
  char c,s[500];
  printf("Enter a sentence (end by '.'):");
  for(i=0;(s[i++]=getchar())!='.';);
  s[i]='\0';
  for(i=0;s[i];i++)
    if(isalpha(s[i]))
{c=tolower(s[i]);
 a[c-'a']++;  
}
  for(i=0;i<26;i++)
    if(a[i])
      printf("Occurrences of \'%c\': %d\n",'a'+i,a[i]);
  return 0;
}

本回答被提问者和网友采纳
第2个回答  2019-02-25
刚入门就有作业啦?