输入
一个句子,首字母需要大写,中间单词的字母都需要用小写。但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;
}