Problem Solving: Reversing the Strings

Reversing the string and its variants

Let’s solve multiple variants of reversing the string:

Exercise 1: Reversing the range

Write a function that takes a string and the start and end indexes of the range as inputs and reverses the string within that range. This function is an extension of this lesson.

Test the function on the provided main in the playground:

Sample input

Before reversing: S = 'THIS is A WORLD OF C++'

Sample output

After reverse(S, 0, 3): S = 'SIHT is A WORLD OF C++'
After reverse(S, 0, 3): S = 'THIS is A WORLD OF C++'
After reverse_all(S):   S= '++C FO DLROW A si SIHT'

Look at the animation below for a better understanding:

The animation below reverses the string in a specific range:

#include <iostream>
#include <string.h>
using namespace std;
void str_reverse_range(char S[ ], int si, int ei)
{
    // write your code here
}
void reverse_all(char S[ ])
{
    str_reverse_range(S, 0, strlen(S)-1);
}
int main()
{
    char S[ ] = "THIS is A WORLD OF C++";
    cout <<"Before reversing \tS='"<< S <<"'"<< endl;
    str_reverse_range(S, 0, 3);
    cout <<" After reversing \tS='"<< S <<"'"<< endl;
    str_reverse_range(S, 0, 3);
    cout <<" After reversing Again\tS='"<< S <<"'"<< endl;
    reverse_all(S);
    cout <<" After reversing All\tS='"<< S <<"'"<< endl;
    return 0;
}




Reversing the range

Look at the solution below:

Exercise 2: Reversing word by word

Write a program that reverses each word in a sentence. The sentence word order should remain the same. Look at the following example:

Sample Program

Before reversing:      S='THIS is A WORLD OF C++'
After reversing All:   S='SIHT si A DLROW FO ++C'
After reversing Again: S='THIS is A WORLD OF C++'

Note: We have words in a sentence with spaces (there can be more than one space in between words), and we want to reverse each word.

Direction

Here is how to solve this problem:

  1. First, we make two-pointer indexes to define the range we would like to reverse (for word-by-word reversal in a loop) the start si=0.

  2. The loop begins with i = si and compares each index of an array and keeps iterating forward until it is on an element, which is a space ' '. Make sure to reverse the last word (as it will not find space. It instead finds '\0', which needs to be handled separately).

  3. As soon as it finds the space, S[si ... i-1] defines a range, which needs to be reversed. For example, we have found the end of the current word, and we’ll reverse it (the word before the space).

  4. We’ll reverse this word.

  5. Now, the loop keeps iterating through i (mimicking that we need to skip all the contiguous spaces), and as soon as we find the next character, that will be the next word’s beginning, hence si=i again.

  6. The above process (steps 3–4) will be repeated until the end of the sentence.

Look at the animation below for word-by-word reversal:

Instruction: Write your code in the following playground.

#include <iostream>
using namespace std;
#include <cstring>
void str_reverse_range(char S[ ], int si, int ei)
{
    int s = si, 
        e = ei;    
    while(s<e)
        swap(S[s], S[e]), s++, e--;
}
void reverse_word_by_word(char S[ ])
{   // Exercise 1
    // write your code
}
void reverse_sentence_words(char S[ ])
{   // Exercise 2
    // write your code here...
}
int main()
{
    char S[ ] = "THIS is A WORLD OF C++";
    cout <<"Before reversing \tS='"<< S <<"'"<< endl;
    reverse_word_by_word(S);
    cout <<" After reversing All\tS='"<< S <<"'"<< endl;
    reverse_word_by_word(S);
    cout <<" After reversing Again\tS='"<< S <<"'"<< endl;

    // Reversing the sentence.
    cout <<"Before sentence reversing \tS='"<< S <<"'"<< endl;
    reverse_sentence_words(S);
    cout <<" After sentence reversing \tS='"<< S <<"'"<< endl;
    reverse_sentence_words(S);
    cout <<" After sentence reversing Again\tS='"<< S <<"'"<< endl;


    return 0;
}




Reversing word by word

Look at the solution below:

Exercise 3: Reversing the words of the sentence

Write a program that reverses each word and sentence in a range.

Sample input

Before reversing        S='THIS is A WORLD OF C++'
After reversing words   S='C++ OF WORLD A is THIS'

The idea is very simple and elegant:

  • First, we’ll call str_reverse_range() to reverse the entire sentence by calling str_reverse_range(S, 0, strlen(S)-1);.
  • Second, we will just call the reverse_word_by_word() function to reverse each word in a sentence.

Look at the animation below (for complete understanding):

Instruction: Write the function, reverse_sentence_words(), in the above playground.