Reverse a String:
Problem Statement: Reverse a string like Sachin Tendulkar to Tendulkar Sachin.
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
}