#单词分析:
我的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char vocabulary[1000];
int shell[1000]={0};
scanf("%s", vocabulary);
int i = 0;
int len = strlen(vocabulary);
for (i = 0; i < len; i++)
{
int d = vocabulary[i]; //对照ascal码值 ;
if (d < 97)
{
d = d + 32; //如果检测到大写字母则将数值加32转化为小写字母;
}
shell[d]++;
}
int count;
int max = shell[97];
int maxnumber = 97;
for (count=98;count<= 122;count++)
{
if (shell[count] > max)
{
max = shell[count];
maxnumber = count;
}
else{
continue;
}
}
printf("%c\n", maxnumber);
printf("%d", max);
return 0;
}
别人的:
#include<stdio.h>
int main()
{
char ch;
int count[26]={0},max=0,i;
while((ch=getchar())!=EOF)
{
count[ch-'a']++;
}
for(i=1;i<26;i++)
{
if(count[i]>count[max])
{
max=i;
}
printf("%c\n%d",max+'a',count[max]);
return 0;
}
值得学习的地方:
1.数组必须初始化,初始化方式如图所示;字符串数组可以不初始化如若初始化可char ch[number]={'\0'};
2.对一段字符串进行处理可以不用创建字符串数组,可以利用用户输入存在缓冲区,而getchar从缓冲区中只读一个字符来挨个处理字符;
3.代码简化:
将printf("%c\n".....);
printf("%d"....);并行为一行;