Description: We have to reverse the ordering of words in a sentence while maintaining the same order of characters within a word.
code to love I
Reverse all characters in the entire string.
This step would align the words in their required positions.
“I love to code”
“edoc ot evol I” ->after step 1
Till now, the words have been aligned in the required positions.
However, the alignment of characters in the words is in reverse order.
Reverse the order of characters within each word.
Now, the sequence within each word is correct.
In our example,
“edoc ot evol I” after step 1
“code to love I” after step 2
O(n), where n is the length of the string.
/* UTILITY FUNCTIONS */
(begin < end)
temp = *begin;
*begin++ = *end;
*end-- = temp;
*wordbeg = NULL;
*temp = s;
/* temp is for word boundary */
/*STEP 1 */
( *temp )
/*This condition is to make sure that the string start with
valid character (not space) only*/
(( wordbeg == NULL ) && (*temp !=
(wordbeg && ((*(temp+1) ==
) || (*(temp+1) ==
wordbeg = NULL;
/*STEP 2 */