int main(){
// 初始化计数器
int upper=0,lower=0,digit=0,space=0,other=0,i=0;
// 指针p用于遍历字符串
char *p,s[20];
// 提示用户输入字符串
printf("请输入字符串: ");
// 获取用户输入,直到遇到换行符
while((s[i]=getchar())!='\n')i++;
// 设置指针p指向字符串的起始位置
p=&s[0];
// 遍历字符串,统计各类字符个数
while(*p!='\n'){
// 统计大写字母
if(('A'<=*p)&&(*p<='Z'))
++upper;
// 统计小写字母
else if(('a'<=*p)&&(*p<='z'))
++lower;
// 统计空格
else if(*p==' ')
++space;
// 统计数字
else if((*p>='0')&&(*p<='9'))
++digit;
// 统计其他字符
else
++other;
// 移动指针到下一个字符
p++;
}
// 打印统计结果
printf("upper case:%d lower case:%d",upper,lower);
printf(" sapce:%d digit:%d other;%d\n",space,digit,other);
return 0;
}
编写一个函数:输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少?
于 2024-04-21 13:49:57 首次发布