| 8 | } |
| 9 | |
| 10 | string reverseWords(string s) { |
| 11 | int start = 0; |
| 12 | for(int i=0;i<s.length();i++){ |
| 13 | if(s[i+1] == ' ' || s[i+1]=='\0'){ |
| 14 | //Since s[i+1] is space or null character, s[i] is end of a word |
| 15 | int end = i; |
| 16 | reverse(s,start,end); |
| 17 | //Since s[i+1] is space or null character, s[i+2] may be the beginning of new word |
| 18 | start = i+2; |
| 19 | } |
| 20 | } |
| 21 | reverse(s,start,s.length()-1); |
| 22 | return s; |
| 23 | } |
| 24 | |
| 25 | int main() |
| 26 | { |