一、问题及代码
/*
*文件名称:字符串
*姓名:许圣凯
*完成日期:2017年5月3日
*版本号:v6.0
*输入描述:asf5asf4DS45 sdfd5
*输出描述: 略
*/
#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[50];
int i=0,n1=0,n2=0,n3=0,n4=0;
cout<<"输入字符串:";
gets(str);
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9')
n1++;
else
if(str[i]>='a'&&str[i]<='z')
n2++;
else
if(str[i]>='A'&&str[i]<='Z')
n3++;
else
n4++;
i++;
}
cout<<"其中的数字个数是: "<<n1<<endl;
cout<<"其中的小写字母个数是:"<<n2<<endl;
cout<<"其中的大写字母个数是:"<<n3<<endl;
cout<<"其他字符数的个数是:"<<n4<<endl;
return 0;
}
二、运行结果
三、心得体会
else是紧跟着上一个if语句,必要的时候要加大括号
四、知识点归纳
循环语句和数组两者之间联系紧密,要学好数组就要将循环语句弄通。