(算法练习)——问题 1012: [编程入门]字符串分类统计

水题,but为什么不是正确!!样例都是对的= =
(答案错误50%)

#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int main(){
	string str[500];//i就是空格数 
	int i = 0,zimu = 0,qita = 0;
	cin>>str[i];
	while(getchar() == ' '){
		i++;
		cin>>str[i];
	}
	int num=0;
	for(int j = 0;j <=i;j++){
		for(int s=0;s <str[j].length();s++){
			if(str[j][s]-'0' >=0 && str[j][s] -'0'<= 9){//判断是否为数字 
				num++;
			}
			else if(str[j][s]>='a'&&str[j][s]<='z' || str[j][s]>='A'&&str[j][s]<='Z'){//判断是否为字母 
				zimu++;
			}
			else{
				qita++;
			}
		}
	}
	printf("%d %d %d %d",zimu,num,i,qita);
}

使用另一种写法,getline(cin,str);,可以包括空格读入str,通过了~
代码:

#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int main(){
	string str;
	getline(cin,str);
	int num = 0,zimu = 0,kongge = 0,qita = 0;
	for(int i = 0;i <str.length();i++){
		if(str[i]-'0' >= 0 && str[i] -'0'<= 9){
			num++;
		}
		else if(str[i]>= 'a' && str[i]<='z' || str[i]>='A' && str[i]<= 'Z'){
			zimu++;
		}
		else if(str[i] == ' '){
			kongge++;
		}
		else{
			qita++;
		}
	}
	printf("%d %d %d %d\n",zimu,num,kongge,qita);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值