张小龙


私信TA

用户名:183020273L

访问量:1244

签 名:

等  级
排  名 13812
经  验 851
参赛次数 1
文章发表 2
年  龄 0
在职情况 学生
学  校 四川大学锦城学院
专  业

  自我简介:

TA的其他文章

解题思路:

1、定义四个变量分别计数不同类型的字符

2、根据每个字符的类型进行判断

        字母:'A' <= str[i] <= 'Z'  ||  'a' <= str[i] <= 'z'

        空格:str[i] == ' '

        数字:'0' <= str[i] <='9'

        其他:else

参考代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str;
	getline(cin,str);
	
	int count1 = 0,  	//字母 
	    count2 = 0, 	//空格 
	    count3 = 0, 	//数字 
	    count4 = 0;		//其他 
	for(int i = 0; i < str.length(); ++i)
	{
		if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) 
			count1++;
		else if(str[i] == ' ')
			count2++;
		else if(str[i]>='0' && str[i]<='9')
			count3++;
		else
			count4++;
	} 
	cout << count1 << endl;	
	cout << count2 << endl;	
	cout << count3 << endl;	
	cout << count4 << endl;	
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区