字符串应用(strcmp函数和strcpy函数)
输入3个字符串,输出其中字符串个数
#include<stdio.h>
void main()
{
char str[3][40]; /*定义1个二维字符数组,表示3行字符串,每个字符串的字符小于40*/
int i,j,lower,upper,digital,space,other; /*定义型变量,用来计数*/
lower=upper=digital=space=other=0; /*初始时,各个计数器为0*/
for(i=0;i<3;i++) /*外层循环表示第几个字符串*/
{
printf("请输入第%d行的字符串:",i+1);
gets(str[i]); /*输入第i个字符串*/
for(j=0;j<40&&str[i][j]!='\0';j++) /*内层循环主要用来判断具体字符的种类*/
{
if(str[i][j]>='a'&&str[i][j]<='z') /*如果是小写字母*/
lower++; /*lower增1*/
else if(str[i][j]>='A'&&str[i][j]<='Z') /*如果是大写字母*/
upper++; /*upper增1*/
else if(str[i][j]>='0'&&str[i][j]<='9') /*如果是数字字符*/
digital++; /*digital增1*/
else if(str[i][j]==32) /*如果是空格符*/
space++; /*space增1*/
else /*如果是1其他字符*/
other++; /*other增1*/
}
}
printf("小写英文字母的个数为%d\n",lower); /*输出小写英文字母的个数*/
printf("大写英文字母的个数为%d\n",upper); /*输出大写英文字母的个数*/
printf("数字字符的个数为%d\n",digital); /*输出数字字符的个数*/
printf("空格字符的个数为%d\n",space); /*输出空格字符的个数*/
printf("其他字符的个数为%d\n",other); /*输出其他字符的个数*/
}