#include <iostream>
#include <cstring>
using namespace std;
void Reverse(char* str,int start,int end){
if(!str) return;
for(int i=start,j=end;i<j;++i,--j){
char tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
char* ReverseSentence(char* str){
if(!str) return NULL;
int len=strlen(str);
//reverse every char
Reverse(str,0,len-1);
//fix every word based on ' '
int start=0,end=0;
while(end<len){
while(str[end]!=' '){
if(str[end]=='\0')
break;
end++;
}
Reverse(str,start,end-1);
start=++end;
}
//fix the last word
Reverse(str,start,end-1);
return str;
}
char* LeftRotate(char* str,int n){
if(!str||n<=0) return NULL;
int len=strlen(str);
int start=0,end=len-1;
//reverse the whole string
Reverse(str,start,end);
//fix the left part
Reverse(str,start,len-n-1);
//fix the right part
Reverse(str,len-n,end);
return str;
}
int main(){
char str[]={"he is a student. "};
char str2[]={"asdfghj"};
cout<<ReverseSentence(str)<<endl;
cout<<LeftRotate(str2,3)<<endl;
}