Search This Blog

Reverse a String: 


Reverse a string

 

Problem Statement: Reverse a string like Sachin Tendulkar to Tendulkar Sachin.


Solution:

Input: String: Sachin Tendulkar

#Pseudo-Code:

        Here,

p1 = point to start of word

p2 = point to end of word or encounter of space after each word

n = length of string

 

// to reverse a single word > character by character

 

          reverse(str , 0 , n-1)   

 

/*     Sachin Tendulkar     >>    rakludneT nihcaS    */

p1=0 ,    p2=0;

while(p1<n)

{

 

    //    iterate on p2 till you reach a space or end

    while(p2<n && str[p2] != ' ')

    {

        p2++;  

    }

 

/*     

Here we again reversing our word which was obtained after first reverse to get required output

 i.e. rakludneT nihcaS    >>    Tendulkar Sachin     

*/ 

    reverse(str , p1 , p2-1)    

    p1 = p2+1;

    p2 = p1;

    // Now both the p1 and p2 are once again pointing to start of new word

}

 

#    if you have any doubt/query feel free to ask in comments or contact me.

No comments

Thank you for your Comment.

Featured post

Calculator Android Application | #Android_Studio #Project

Calculator Android Application | #Android_Studio #Project A calculator is a device that performs arithmetic operations on numbers. This App ...

Popular Posts

Stay Tuned

Connect on Facebook

Subscribe on YouTube

About this Site

I have come up with this website to share what I do and what I am. Therefore, I invite you to go through this website to understand my work and also services that I provide.