Solution
#include <iostream>
using namespace std;
void revert_str(char *start, char* end)
{
while(*end != '\0' && *start != '\0' && end > start){
char t = *start;
*start = *end;
*end = t;
start ++;
end --;
}
}
bool is_letter(char l)
{
if((l >= 'a' && l <= 'z')||(l >= 'A' && l <= 'Z')){
return true;
}
else{
return false;
}
}
void revert_sentence(char *str)
{
char *start = str;
char *end = str;
if(str == NULL){
return ;
}
while(*end != '\0'){
end ++;
}
end--;
while(*end != '\0' && *start != '\0' && end > start){
char t = *start;
*start = *end;
*end = t;
start ++;
end --;
}
start = str;
while(*start != '\0'){
end = start;
while(is_letter(*end)){
end ++;
}
end --;
revert_str(start, end);
end ++;
start = end;
while(*start !='\0' && !is_letter(*start)){
start ++;
}
}
}
int main(int argc, char* argv[])
{
char str[100] = "I come from London.";
revert_sentence(str);
cout << str << endl;
return 0;
}
Problem
Reverse a sentence but do not reverse words and period.
For example:
I come from London.
.London from come I