How to Reverse a String in C++ using While Loop
A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer met.
The syntax for the while loop:
The syntax for a while loop is given by:
while(condition)
{
statement(x);
}
Strings
In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string).
The characters of a string are stored as a collection of bytes in contiguous memory regions by the string class. Strings are most typically employed in programs that need text manipulation. In C++, we may operate a variety of operations on strings. For instance, reversing, concatenating, or sending a function as an argument.
Syntax
The syntax for generating a string in C++ is simple. In C++, the string keyword is used to generate a string. Before we can use this keyword, we must first include the standard string class in our program.
string str_ name = "this is C++ string";
Example of Declare and Initialize string:
string str_ name = "hello";
(or)
string str_ name("hello");
- C++ also supports C-style strings.
Syntax:
Char str_name[ ];
String reverse using for loop:
The reverse of a string can be done using different ways:
Method 1: C++ string reverse using reverse ()
- reverse() is a function in the algorithm header file that reverses a sequence within a defined range. We use the reverse () function and include the <algorithm> header file.
Program to print reverse of string using REVERSE ()
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str = "Java-TPoint";
reverse(str.begin(), str.end());
cout << str << endl;
}
The output of the program:
tnioPT-avaJ
Method 2: C++ String Reverse by Swapping Characters:
Algorithm:
Step 1: Start.
Step 2: Take a string and store it in the variable str.
Step 3: Initialize variable index with 0.
Step 4: Check to see if the index is less than half of the str's length. If its not true, go to step 7.
Step 5: Str[index] should be swapped by str[str length - 1 - index].
Step 6: Increment index, go to step 4.
Step 7: Stop.
Program to print the reverse of a string by swapping characters:
#include <iostream>
using namespace std;
int main()
{
string str = "Java-TPoint";
char ch;
int index=0, length;
while(length=str.length())
{
ch = str[index];
str[index] = str[length-1-index];
str[length-1-index] = ch;
index++;
}
cout<<str;
}
The output of the program:
tnioPT-avaJ
Method 3:
This method includes the two pointers, one at the beginning and the other at the end of the sequence. The characters are reversed one by one with the help of these pointers.
Program to print reverse of string:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[] = "javatpoint";
cout << "Original string: " << str;
cout << endl << "String after reverse: ";
int i = (strlen(str) - 1);
while(i>=0){
cout << str[i];
i--;
}
return 0;
}
The output of the program:
tnioptavaj
Method 4: Using a first-to-last approach ‘WHILE-LOOP’
#include<iostream>
using namespace std;
// Function to reverse a string //
void reverseStr(string& str)
{
int n = str.length();
int j;
// Swap character starting from two corners //
int i=0;
while(i<j) {
int j=n-1;
while(j<n){
swap(str[i], str[j]);
j--; }
i++;}
}
// main program //
int main()
{
string str = "javatpoint";
reverseStr(str);
cout<<str;
return 0;
}
Output:
tnioptavaj
Method 5: C++ String Reverse using Recursion:
Recursion: In C++, recursion is a method that calls itself, either directly or indirectly, until a certain condition is met. This technique has a base case and a recursive condition. It continuously calls the function within the same function. The recursive condition aids in the repeated execution of code, whereas the base case aids in the condition's termination.
If the recursive function does not have a base case, the recursive function will continue to repeat itself indefinitely.
Program:
#include <iostream>
using namespace std;
void strreverse(string& str, int n, int i)
{
if (n <= i)
{
return;
}
swap(str[i], str[n]);
strreverse(str, n - 1, i + 1);
}
int main()
{
string str = "javatpoint";
strreverse(str, str.length() - 1, 0);
cout << str << endl;
}
Output:
tnioptavaj
A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer met.
The syntax for the while loop:
The syntax for a while loop is given by:
while(condition)
{
statement(x);
}
Strings
In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string).
The characters of a string are stored as a collection of bytes in contiguous memory regions by the string class. Strings are most typically employed in programs that need text manipulation. In C++, we may operate a variety of operations on strings. For instance, reversing, concatenating, or sending a function as an argument.
Syntax
The syntax for generating a string in C++ is simple. In C++, the string keyword is used to generate a string. Before we can use this keyword, we must first include the standard string class in our program.
string str_ name = "this is C++ string";
Example of Declare and Initialize string:
string str_ name = "hello";
(or)
string str_ name("hello");
- C++ also supports C-style strings.
Syntax:
Char str_name[ ];
String reverse using for loop:
The reverse of a string can be done using different ways:
Method 1: C++ string reverse using reverse ()
- reverse() is a function in the algorithm header file that reverses a sequence within a defined range. We use the reverse () function and include the <algorithm> header file.
Program to print reverse of string using REVERSE ()
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str = "Java-TPoint";
reverse(str.begin(), str.end());
cout << str << endl;
}
The output of the program:
tnioPT-avaJ
Method 2: C++ String Reverse by Swapping Characters:
Algorithm:
Step 1: Start.
Step 2: Take a string and store it in the variable str.
Step 3: Initialize variable index with 0.
Step 4: Check to see if the index is less than half of the str's length. If its not true, go to step 7.
Step 5: Str[index] should be swapped by str[str length - 1 - index].
Step 6: Increment index, go to step 4.
Step 7: Stop.
Program to print the reverse of a string by swapping characters:
#include <iostream>
using namespace std;
int main()
{
string str = "Java-TPoint";
char ch;
int index=0, length;
while(length=str.length())
{
ch = str[index];
str[index] = str[length-1-index];
str[length-1-index] = ch;
index++;
}
cout<<str;
}
The output of the program:
tnioPT-avaJ
Method 3:
This method includes the two pointers, one at the beginning and the other at the end of the sequence. The characters are reversed one by one with the help of these pointers.
Program to print reverse of string:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[] = "javatpoint";
cout << "Original string: " << str;
cout << endl << "String after reverse: ";
int i = (strlen(str) - 1);
while(i>=0){
cout << str[i];
i--;
}
return 0;
}
The output of the program:
tnioptavaj
Method 4: Using a first-to-last approach ‘WHILE-LOOP’
#include<iostream>
using namespace std;
// Function to reverse a string //
void reverseStr(string& str)
{
int n = str.length();
int j;
// Swap character starting from two corners //
int i=0;
while(i<j) {
int j=n-1;
while(j<n){
swap(str[i], str[j]);
j--; }
i++;}
}
// main program //
int main()
{
string str = "javatpoint";
reverseStr(str);
cout<<str;
return 0;
}
Output:
tnioptavaj
Method 5: C++ String Reverse using Recursion:
Recursion: In C++, recursion is a method that calls itself, either directly or indirectly, until a certain condition is met. This technique has a base case and a recursive condition. It continuously calls the function within the same function. The recursive condition aids in the repeated execution of code, whereas the base case aids in the condition's termination.
If the recursive function does not have a base case, the recursive function will continue to repeat itself indefinitely.
Program:
#include <iostream>
using namespace std;
void strreverse(string& str, int n, int i)
{
if (n <= i)
{
return;
}
swap(str[i], str[n]);
strreverse(str, n - 1, i + 1);
}
int main()
{
string str = "javatpoint";
strreverse(str, str.length() - 1, 0);
cout << str << endl;
}
Output:
tnioptavaj