C++基础练习

1》提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数

     1	#include<iostream>
     2	using namespace std;
     3	
     4	int main()
     5	{
     6		string str1;      //定义字符串数据
     7		cout << "请输入一个字符串>>>" ;
     8		getline(cin,str1);       //输入一个字符串数据
     9	
    10		int zf=0;     //定义变量统计字符个数
    11		int num=0;    //定义变量统计数组个数
    12		int kong=0;   //定义变量统计空格个数
    13		int other=0;   //定义变量统计其他字符个数
    14		int len=str1.length();   //字符串长度
    15		for(int i=0;i<len;i++)
    16		{
    17	
    18			if((str1[i] >= 'A' && str1[i] <= 'Z')||(str1[i] >= 'a' && str1[i] <= 'z'))
    19			{
    20				zf++;
    21			}
    22			else if(str1[i]==' ')
    23			{
    24				kong++;
    25			}
    26			else if(str1[i]>='0' && str1[i]<='9')
    27			{
    28				num++;
    29			}
    30			else
    31			{
    32				other++;
    33			}
    34		}
    35		cout<<"字母:"<<zf<<endl;
    36		cout<<"数字:"<<num<<endl;
    37		cout<<"空格:"<<kong<<endl;
    38		cout<<"其他字符:"<<other<<endl;
    39		return 0;
    40	}
    41	

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值