思路:输入一行就进行统计,然后再输入下一行
代码:
#include<stdio.h>
int main() {
char text[3][20];//定义三行,每行最多不超过20个字符的二位数组
char c;
int i,j;
int upCase,lowCase,num,space,others;
upCase=lowCase=num=space=others=0;
for(i=0;i<3;i++){
gets(text[i]);
for(j=0;j<20&&((c=text[i][j])!='\0');j++){
if(c>='A'&&c<='Z'){
upCase++;
}else if(c>='a'&&c<='z'){
lowCase++;
}else if(c>='0'&&c<='9'){
num++;
}else if(c==' '){
space++;
}else {
others++;
}
}
}
printf("upCase=%d,lowCase=%d,num=%d,space=%d,others=%d",upCase,lowCase,num,space,others);
return 0;
}
注意:
- ‘\0’和”\0”是不一样的,因为char数组中的元素是字符型
- (text[i][j]>=’0’)&&(text[i][j]<=’9’)不能写成(text[i][j]>=0)&&(text[i][j]<=9),因为比较的是字符型,而不是数字