C++

Reverse Words in a String Using C++

A string variable consists of a collection of characters surrounded by double quotations. This C++ article will go over how to reverse words in a string using any built-in and other functions.

Reversing or entirely flipping a string in C++ implies changing the sequence of the characters in the string to appear backward. Depending on the needs of the program you’re implementing, we can use a variety of implementations to reverse a string.

Example 1: Use of the Built-In Reversing Function for Reversing Words in a String in C++

C++ includes a reverse function for reversing strings. The string starting iterator and the string end iterator are the only two parameters accepted by this function. The subsequent line of code shows the usage of this function.

Initially, we have included the header file in the code. The iostream file and the stdc file. This stdc file will make the reverse string function here. The std namespace file is also provided, enabling the use of its classes and functions in the code.

Then, we have the program’s main function, and in the main function body, we have a string variable declaration as “StrValue”. At the same time, we have initialized it with the word. The output string will be displayed through the C++ cout command. After this, we use a function named “reverse”. The function “reverse” intakes two parameters. The first parameter is the begin(), and the second parameter is the end() iterator which iterates over the specified string of words. The begin() function returns an iterator referring to the container’s initial element.

On the other hand, the end() iterator returns an iterator referring to the container’s last element. The reversed string word will be printed after the reversing function.

#include <iostream>

#include<bits/stdc++.h>

using namespace std;
int main() {
  string StrValue = "Excellent";
cout<<"String:"<<StrValue<<endl;

reverse(StrValue.begin(),StrValue.end());
cout<<"Reverse String:"<<StrValue<<endl;
}

The outcomes of the string, as well as the inverted string, are shown in the following image:

Example 2: Use of for Loop for Reversing Words in a String in C++

A loop is useful for reversing a string. To modify the locations of elements, we will use the swap function, which is a built-in method of C++. Let’s discuss the following code snippet to reverse a string.

We have added the header files in the code. In the next step, we have invoked the main function, wherein we implement the code for reversing a string. First, we have defined a string variable “MyString”. The string variable “MyString” contains a word of string “kalsoom”, on which we have to apply the reverse method. We have displayed the string with the cout statement. Then, we have declared an int variable “strlen”, which has called the length function for the given string. We have also declared another int variable “s” to keep the “strlen-1”. The “strlen-1” procedure needs the string’s length up to the character at the last position.

Then, we have a swap algorithm where “temp” is used to store the elements of the “char” at the index “i” so that we can swap it out with the char elements at index “s” later. The reversed string of this program is then displayed. After that, we have a for loop condition which will reverse the word of the specified string.

#include <iostream>

using namespace std;
int main() {
  string MyString = "Kalsoom";
cout<<"Original String: "<<MyString<<endl;
  int strlen = MyString.length();
  int s =strlen-1;
for(int i=0;i<(strlen/2);i++){
char temp = MyString[i];
MyString[i] = MyString[s];
MyString[s] = temp;
s = s-1;
  }
cout<<"Reversed String: "<<MyString<<endl;
}

You can see the original string “kalsoom” and the reverse of the given string on the Ubuntu prompt.

Example 3: Use of a Function for Reversing Words in a String in C++

Recursion is also utilized to make a reverse string function. The subsequent code illustration demonstrates how we can reverse the string via function.

In the first step, we have a function called “RevStr”, and the constructor is also generated for this function. The constructor takes the string reference, the integer variable “i”, and the other integer variable “j”. In the function “RevStr”, we have the if condition and the swap function, which swaps the index “i” with the index “j”. The function “RevStr” is called for the next word of the string to reverse.

In the end, the main function has the cout command used to print the string before the reverse method and after the reverse method.

#include <iostream>

using namespace std;
void RevStr(string& str, int i,int j){
  if(i<=j){return;}
  swap(str[i] ,str[j]);
RevStr(str ,i-1,j+1);
}
int main() {
  string MyStr = "Linux";
cout<<"String: "<<MyStr<<endl;
RevStr(MyStr,MyStr.length()-1,0);
cout<<"Reversed String: "<<MyStr<<endl;
}

The following screenshot shows the output of the string before and after applying the function:

Example 4: Creating a New String for Reversing Words in a String in C++

Looping backward over a string and keeping the contents in a new string of the same size is a roundabout approach for reversing it. The push-back() method can add characters to an empty string.

In the following program’s main function, we have declared a string variable “StringOne” and stored a word of string in it. Then, we have declared another variable, “new_String”. The for loop is used for the string variable “StringOne”, which iterates over the last character in the string and returns the string in reverse order. Then, with the push_back() method, we have added the string value returned from the for loop into the new_String. In the end, the string and the reversed string will be printed.

#include <iostream>

using namespace std;
int main() {
  string StringOne = "programming";
  string new_String;
for(int s = StringOne.length()-1; s >= 0; s--){
new_String.push_back(StringOne [s]);
  }
cout<<"String: "<<StringOne<<endl;
cout<<"Reversed string: "<<new_String<<endl;
}

The following results are shown that the string is reversed upon the previous program’s execution:

Conclusion

Here, we reversed the string of words by exploring the possible ways to reverse the string in C++. We have reversed the string with the C++ built-in function, the loop method and function, and the creation of a new string. All the outcomes obtained from these methods of reversing string methods have proven results by using the C++ compiler in Ubuntu 20.04.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content