reverse
class Solution {
public:
//反转单词前缀
string reversePrefix(string word, char ch) {
int n=word.size();
for(int i=0;i<n;i++){
if(word[i]==ch){
reverse(word.begin(),word.begin()+i+1);
break;
}
}
return word;
}
};
substr
class Solution {
public:
//反转单词前缀
string reversePrefix(string word, char ch) {
int n=word.size();
bool flag=false;
int i;
string ans;
string tmp1,tmp2;
for(i=0;i<n;i++){
if(word[i]==ch){
tmp1=word.substr(0,i+1);
tmp2=word.substr(i+1,n);
flag=true;
break;
}
}
if(flag){
reverse(tmp1.begin(),tmp1.end());
ans=tmp1+tmp2;
}
else{
ans=word;
}
return ans;
}
};