6

how to reverse a string(not character array) using inbulit function in c++.And i need to store original copy as well as reversed one so that i can compare them for equality.please let me know how to do this

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
sneha sharma
  • 95
  • 1
  • 1
  • 8

5 Answers5

9
#include <algorithm>

std::string str1("original");
std::string str2(str1);
std::reverse(str2.begin(), str2.end());

if(str1 == str2)...

Take a copy of the original string, then use std::reverse to inplace reverse the copy. Then you can do a comparison on the two.

const_ref
  • 4,016
  • 3
  • 23
  • 38
1

You could simply use the reverse() function in <algorithm>.

std::string same("Hello world");
std::string reversed(same);
std::reverse(reversed.begin(),reversed.end());
//To compare them for equality..
if (same == reversed) {
...
}
shauryachats
  • 9,975
  • 4
  • 35
  • 48
0

Make a copy of the string and then use the reverse function from the algorithm header.

std::string original;
// put data in  the string
std::string reversed(original);
std::reverse(reversed.begin(), reverse.end());
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Are you sure that this helps the OP or any other reader. – Cheers and hth. - Alf Feb 05 '15 at 13:49
  • Hm, sorry for picking on just your answer, I see now a host of other trivial answers appear. I once answered a question about how to change the value of a variable, by pointing out that the assignment operator `=` does that. It was mainly irony but that aspect was lost on the readers, who upvoted it as a fine answer. Such days I almost despair. People have just stopped thinking. – Cheers and hth. - Alf Feb 05 '15 at 13:52
  • There really isn't any other way to answer what the OP asked. They want to use a standard function and keep the original as well. Should I have explained it more? – NathanOliver Feb 05 '15 at 13:54
  • Rather, less explation would IMHO be better. E.g. pointing out that assignment or initialization makes copy, and `std::reverse` can reverse -- at most. But even better (now that I'm thinking about it) just using a comment to point the OP towards some documentation set such as (http://cppreference.com), and hinting about maybe something called "reverse". – Cheers and hth. - Alf Feb 05 '15 at 13:58
  • @Cheersandhth.-Alf Surely that defeats the point of having answers to questions if people just comment instead of answer? When flicking through old stack overflow questions people will look first at answers then to comments so these are more helpful IMHO. – const_ref Feb 05 '15 at 14:00
0

std::reverse operates in place, so if you want to keep both the original and the reversed strings, you will have to make a copy first, and then reverse it:

std::string original("foo");
std::string copy(original);
std::reverse(copy.begin(), copy.end());
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
-3

You do not need keep separate copy, you can access string in reverse order as follows:

string str = "Well Come";
for (unsigned i = str.size() - 1; i >= 0; i++)
    cout << str.at(i);
msrd0
  • 7,816
  • 9
  • 47
  • 82
Ali
  • 1