//
// main.cpp
// Sort
//
// Created by scuhmz on 4/29/15.
// Copyright (c) 2015 com.nwpu.hmz. All rights reserved.
//
#include <iostream>
char* ReverseStr (char* s,int from,int to){
while (from < to) {
char t = s[from];
s[from++] = s[to];
s[to--] = t;
}
return s;
}
char* ShiftStr(char* s){
int length = (int)strlen(s);
char * re = ReverseStr(s, 0, length-1);
int left = 0;
int right = 0;
while (right<length) {
right++;
if (re[right] == ' '||re[right]=='\0') {
re = ReverseStr(re , left, right-1);
left = right+1;
}
}
return re;
}
int main(int argc, const char * argv[]) {
char test[] = "i am a student.";
ShiftStr(test);
printf("%s\n",test);
return 0;
}
【算法】单词翻转
最新推荐文章于 2022-12-02 14:48:38 发布