Spread the post

Reverse each word in a string

Write an efficient algorithm to reverse words in a given string or reverse a sentence without reversing the words. Note that handle the case if given string are start by space.

Example 1:

Input String: Programming C Learn To Ready You Are

Output String: Are You Ready To Learn C Programming

Example 2:

Input String: code to like you Are

Output String: Are you like to code

Function

This function accept two parameters.

1) First index address of string

2) One stack double pointer

view given string in array.

Stack Areamainarray [0] c [1] o [2] d [3] e [4] [5] t [6] o [7] [8] l [9] i [10] k [11] e [12] [13] y [14] o [15] u [16] [17] A [18] r [19] e [20] \0top(pointer) NULL

View stack data.

Stack Areamainarray [0] c [1] o [2] d [3] e [4] [5] t [6] o [7] [8] l [9] i [10] k [11] e [12] [13] y [14] o [15] u [16] [17] A [18] r [19] e [20] \0top(pointer)reverse_wordcounter (int) =2index (int) =19temp(pointer)top(pointer)fill_dataend (int) =17start (int) =17temp(pointer)top(pointer) Heap Areastruct Stackdata (char)= next (pointer)= NULLstruct Stackdata (char)= enext (pointer) struct Stackdata (char)= dnext (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= cnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= tnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= enext (pointer) struct Stackdata (char)= knext (pointer) struct Stackdata (char)= inext (pointer) struct Stackdata (char)= lnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= unext (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= ynext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= enext (pointer) struct Stackdata (char)= rnext (pointer) struct Stackdata (char)= Anext (pointer)

Program for reverse a sentence without reversing the words in c. Time complexity O(n).


Output

Stack Areamainarray [0] c [1] o [2] d [3] e [4] [5] t [6] o [7] [8] l [9] i [10] k [11] e [12] [13] y [14] o [15] u [16] [17] A [18] r [19] e [20] \0top(pointer)reverse_wordcounter (int) =2index (int) =19temp(pointer)top(pointer)fill_dataend (int) =18start (int) =17temp(pointer)top(pointer)pushnew_node(pointer)top(pointer)value (char) =r Heap Areastruct Stackdata (char)= next (pointer)= NULLstruct Stackdata (char)= enext (pointer) struct Stackdata (char)= dnext (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= cnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= tnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= enext (pointer) struct Stackdata (char)= knext (pointer) struct Stackdata (char)= inext (pointer) struct Stackdata (char)= lnext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= unext (pointer) struct Stackdata (char)= onext (pointer) struct Stackdata (char)= ynext (pointer) struct Stackdata (char)= next (pointer) struct Stackdata (char)= enext (pointer) struct Stackdata (char)= rnext (pointer)= ?

Execution Process Try it Yourself

Spread the post

Recommended Posts: