Sample input String = "Ajitesh is a good boy"
Sample output String="boy good a is Ajitesh"
string wordReverse(string str) { int i = str.length() - 1; int start, end = i + 1; string result = "" ; while (i >= 0) { if (str[i] == ' ' ) { start = i + 1; while (start != end) result += str[start++]; result += ' ' ; end = i; } i--; } start = 0; while (start != end) result += str[start++]; return result; } // main code int main() { string str = "Ajitesh is a good boy" ; cout << wordReverse(str); return 0; } |