原题链接:https://leetcode-cn.com/problems/reverse-prefix-of-word/
class Solution {
public:
string reversePrefix(string word, char ch) {
//找出下标i
int n = word.size();
int index = 0;
while (index < n) {
if (word[index] == ch) {
break;
}
++index;
}
if (index == n) {
return word;
}
//逆转字符串
for (int i = 0, j = index; i < j; ++i, --j) {
int tmp = word[i];
word[i] = word[j];
word[j] = tmp;
}
return word;
}
};