Reverse a string in C++
Chances are you’ll use alternative ways for reversing a string in C++. This contains built-in capabilities that you could be use fairly simply and it’s possible you’ll write your personal operate as effectively.
First manner – Utilizing reverse operate instance
- There’s a built-in operate reverse() that you could be use to reverse a string in C++.
- That is outlined within the algorithm header file that it’s important to embrace within the C++ file.
- The reverse operate takes two parameters as proven within the instance under
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#embrace <algorithm>
#embrace<iostream>
#embrace<string>
utilizing namespace std;
int most important()
{
//Making a string to reverse
string str_rev = “ABC DEF GHI JKL MNO PQR STU VWX YZ”;
cout <<“The unique string: “ <<str_rev <<“nn”;
//Utilizing reverse operate reverse(str_rev.start(), str_rev.finish());
//Show end result cout <<“The string after reverse operate: “ <<str_rev <<“nn”;
return 0;
} |
Output:
In this system,
- A string variable is said and assigned a string containing A-Z.
- We used the reverse() operate with start and finish parameters
- The string is displayed earlier than and after utilizing the reverse() operate.
Utilizing for loop with swap() operate
On this instance, the swap() operate is used to reverse a string the place a for loop is used. See the code and output under:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#embrace <bits/stdc++.h> utilizing namespace std;
//Person outlined operate that takes one parameter i.e. string to reverse
void rev_function(string& s){
int str_len = s.size();
//For loop that makes use of swap operate
for (int x = 0; x < str_len / 2; x++)
swap(s[x], s[str_len – x – 1]);
}
int most important(){
string str_rev = “Hiya World! C++”;
cout <<“The unique string: “ <<str_rev <<“nn”;
rev_function(str_rev);
cout <<“The unique string: “ <<str_rev <<“nn”;
cout << str_rev;
return 0;
} |
Output:
You see, we despatched “Hiya World! C++” to the operate and it reversed the string.
The swap() operate is used to swap the characters.
Study extra concerning the swap operate right here.
Studying string final to first – for loop
On this strategy, a for loop is used and the string is learn from final to first to get the reversed string.
Take a look on the code and output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#embrace<iostream>
#embrace<string>
utilizing namespace std;
int most important()
{
string str = “String Reverse Tutorial”;
int i = str.size();
cout <<“Reversed String: “;
for(i = i – 1; i >= 0; i—)
{
cout<<str[i];
}
cout <<“nnOriginal String: “ <<str <<“nn”;
return 0;
} |
Output:
The logic is:
- First, you get the size of the string and assign it to an int-type variable within the for loop.
- The variable i worth is decremented in every iteration
- The output exhibits a reverse string, in addition to authentic string, stays the identical
The instance of utilizing some time loop for reversing the string
In case you are a fan of the whereas loop, the identical factor could be achieved as within the above instance by utilizing whereas loop.
See this system under the place the consumer is requested to enter a string to be reversed.
The whereas loop is used to reverse it from final to first and we used getline() operate with cin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#embrace<iostream>
#embrace<string>
utilizing namespace std;
int most important()
{
string str;
//Taking consumer enter for the string to be reversed
cout<< “Please enter a string to reverse? “;
//Utilizing getline as easy cin will minimize from house
getline(cin, str);
int i = str.size() – 1;
cout<<“Your entered string: “ <<str <<“nn”;
cout <<“Reversed String: “;
//whereas loop to reverse the string
whereas (i >= 0) {
cout<<str[i];
i—;
}
cout <<“nnOriginal String: “ <<str <<“nn”;
return 0;
} |
Pattern output:
Reverse string by utilizing do-while loop
And eventually, the identical could be achieved by utilizing a do-while loop as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#embrace<iostream>
#embrace<string>
utilizing namespace std;
int most important()
{
string str;
//Taking consumer enter for the string to be reversed
cout<< “Please enter a string to reverse? “;
//Utilizing getline as easy cin will minimize from house
getline(cin, str);
int i = str.size() – 1;
cout<<“Your entered string: “ <<str <<“nn”;
cout <<“Reversed String: “;
//Utilizing do-while
do{
cout<<str[i];
i—;
}
whereas (i >= 0) ;
cout <<“nnOriginal String: “ <<str <<“nn”;
return 0;
} |
Pattern output: