C语言编程在线等急
发布网友
发布时间:2022-04-22 00:10
我来回答
共2个回答
热心网友
时间:2023-09-14 02:12
#include <stdio.h>
#define MAXSIZE 1024
int main() {
char s[MAXSIZE];
int i,digits[10] = {0},alpha[26] = {0},others = 0;
printf("输入一个字符串:");
scanf("%s",s);
for(i = 0; s[i] != '#';++i) {
if(s[i] >= '0' && s[i] <= '9') ++digits[s[i] - '0'];
else if(s[i] >= 'A' && s[i] <= 'Z') ++alpha[s[i] - 'A'];
else if(s[i] >= 'a' && s[i] <= 'z') ++alpha[s[i] - 'a'];
else ++others;
}
for(i = 0; i < 10; ++i) {
if(digits[i]) printf("'%d':%d\n",i,digits[i]);
}
printf("\n");
for(i = 0; i < 26; ++i) {
if(alpha[i]) printf("'%c':%d\n",i + 'A',alpha[i]);
}
printf("\n");
if(others) printf("others:%d\n",others);
return 0;
}
热心网友
时间:2023-09-14 02:12
#include<stdio.h>
#define N 30
char str[N+1];
int char_num[N];
void main(void)
{
int i=0;
gets(str); //使用gets,防止遇到空格符停止
while(str[i] != '#')
{
if(str[i] != ' ') //忽略空格
char_num[str[i]-'A']++; //A ->char_num[0],B ->char_num[1]...
i++;
}
for(i=0;i<N;i++)
{
if(char_num[i]) //只打印有效数据
printf("%c:%d\n",'A'+i,char_num[i]);
}
}
ABCDEABC#
A:2
B:2
C:2
D:1
E:1
代码缺陷:
①只能计算大写字母个数
②最大输入字符串个数26个(随意定义的)