问题描述
编程统计输入字符串中的字符数。
代码实现:
#include<stdio.h>
#include<string.h>
void main(){
char str[3][100];
int up=0,low=0,space=0,num=0,other=0,i,j;
//输入字符串
for(i=0;i<3;i++){
printf("input the %dth string:",i+1);
gets(str[i]);
}
//统计
for(i=0;i<3;i++){
for(j=0;j<strlen(str[i]);j++){
if(str[i][j]>='A' && str[i][j]<='Z')
up++;
else if(str[i][j]>='a' && str[i][j]<='z')
low++;
else if(str[i][j]>='0' && str[i][j]<='9')
num++;
else if(str[i][j]==' ')
space++;
else
other++;
}
}
printf("经统计,大写字母,小写字母,数字,空格,其他字符分别有:\n");
printf("up=%d,low=%d,num=%d,space=%d,other=%d\n",up,low,num,space,other);
}
代码运行截图: