Reversing a string c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reversing a string c++

So in this case, the goal is to take a string,and output the string but reversed. how I've attempted to do this is to place the letters of the original string into a char array. which worked. but where it went wrong was I am trying to concatenate the contents of the array, into a string that will be the reversed version https://code.sololearn.com/cvaxwvq5hylt/?ref=app

10th Sep 2017, 7:23 PM
K1llJ0Y
K1llJ0Y - avatar
6 Answers
+ 3
#include <iostream> using namespace std; int main(){ string str = "abc"; for(int i=1;i <= str.size();i++){ cout<<str[str.size()-i]; } return 0; } https://code.sololearn.com/ciWmy0p34SoH/?ref=app
10th Sep 2017, 8:44 PM
Oma Falk
Oma Falk - avatar
+ 3
simple, use reverse function. string str = "abcde";    // Reverse str[beign..end]    reverse(str.begin(),str.end());    cout << str; //output- edcba
10th Sep 2017, 9:11 PM
Aqua9
Aqua9 - avatar
+ 3
you need to import "algorithm" you can also refer to this. http://www.geeksforgeeks.org/quickly-reverse-string-c/
10th Sep 2017, 10:28 PM
Aqua9
Aqua9 - avatar
+ 3
Shawn ist right
11th Sep 2017, 6:12 AM
Oma Falk
Oma Falk - avatar
0
Iridescent995 is there some syntax you didn't specify? because it says something about it not being declared in the scope
10th Sep 2017, 9:49 PM
K1llJ0Y
K1llJ0Y - avatar
0
I did import algorithm, or is it not #include <algorithm> and is something else
10th Sep 2017, 11:28 PM
K1llJ0Y
K1llJ0Y - avatar