|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如图所示,
我只知道有while(scanf("%d", &a) != EOF)这种写法,
但是字符串的gets好像不能这样写……
所以我应该怎么写呢?
参考
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LINE 100
- #define CHARNUM 1000
- void printAlpha(char *str);
- int main(void)
- {
- char *sArr[LINE] = {NULL};
- char *s = NULL;
- char ch;
- int i = 0, j = 0;
- // 获取输入 ! 表示结束
- while ( (ch=getchar()) != '!' )
- {
- s = (char *) malloc( sizeof(char) * CHARNUM );
- memset(s, '\0', sizeof(char) * CHARNUM);
- i = 0;
- while( (ch=getchar()) != '\n' )
- {
-
- s[i++] = ch;
- }
- sArr[j++] = s;
- }
- // 打印输出
- i = 0;
- while( (s=sArr[i++]) != NULL)
- {
- printAlpha(s);
- }
-
- return 0;
- }
- void printAlpha(char *str)
- {
- int i = 0, j, max;
- int sArr[26] = {0};
- char s;
- // 统计
- while( (s=str[i++]) != '\0' )
- {
- if ( (s>='A') && (s<='Z') )
- {
- // 大写
- sArr[s - 65] += 1;
- }
- else if ( (s>='a') && (s<='z') )
- {
- // 小写
- sArr[s - 97] += 1;
- }
- }
- // 找出现次数最多的
- max = 0;
- j = 0;
- for( i=0; i<26; i++)
- {
- if ( sArr[i]>max )
- {
- max = sArr[i];
- j = i;
- }
- }
- printf("%c = %d\n", 65+j, max);
- }
复制代码
|
|