#include <iostream>
using namespace std;
//先整体逆置,再逐词逆置
void wordReverse(char* s,int len) {
char temp;
int l = 0;
int r = len - 1;
while( l < r) {
temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
l = r = 0;
while(true) {
r++;
if(s[r] == ' ' || s[r] == '\0'){
int tempR = r;
r--;
while(l < r) {
temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
if(s[tempR] == '\0')
return;
l = tempR;
while(s[l] == ' ')
l++;
r = l;
}
}
}
int main(){
char s[] = "what the hell is going on";
wordReverse(s,strlen(s));
cout<<s<<endl;
return 0;
}
将字符串中的单词翻转
最新推荐文章于 2022-11-01 17:13:23 发布