单词纠正 (2017网安)

输入

一个句子,首字母需要大写,中间单词的字母都需要用小写。但i和bupt不管在哪里都需要大写。

输出

输出纠正后的句子

样例输入

i loVe bupt
hEllO wORlD

样例输出

I love BUPT
Hello world
#include<bits/stdc++.h>
using namespace std;
char str[1005];
int main() {
	while(cin.getline(str,1005)) {
		for(int i=0; i<strlen(str); i++) { //全部变为小写
			if(str[i]>'A'&&str[i]<'Z')
				str[i]=str[i]+32;
			if(str[i]=='i') {//I
				str[i]=str[i]-32;
			}
		}
		for(int i=0; i<strlen(str)-3; i++) {//BUPT
			if(str[i]=='b'&&str[i+1]=='u'&&str[i+2]=='p'&&str[i+3]=='t') {
				for(int j=0; j<4; j++) {
					str[i+j]=str[i+j]-32;
				}
			}
		}
		if(str[0]>'a'&&str[0]<'z') {//首字母
			str[0]=str[0]-32;
		}
		cout<<str<<endl;
	}
}

使用string的find函数:

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	getline(cin,s);
	for(int i=0;i<s.size();i++){//转化为小写 
		s[i]=tolower(s[i]);
	}
	int pos=0;
	while((pos=s.find("i",pos))!=string::npos){
		s.replace(pos,1,"I");
	}
	pos=0;
	while((pos=s.find("bupt",pos))!=string::npos){
		s.replace(pos,4,"BUPT");
	}
	s[0]=toupper(s[0]);
	cout<<s<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值