-2

I am trying to make a palindrome checker. So in order to tackle this problem I figured I would compare the original string to the reversed one. However how can I save the reversed string without overwriting the old one?

reverse(myString.begin(), myString.end()) will reverse my string but it replaces myString and doing string reversedString = reverse(myString.begin(), myString.end()); does not work. Here is what I have so far:

int main()
{
    cout << "Enter a string: ";
    string myString;
    getline(cin, myString);
    cout << endl;

    string reversedString = reverse(myString.begin(), myString.end());

    cout << reversedString;

    return 0;
}
XBlueCode
  • 785
  • 3
  • 18
Konachan
  • 11
  • 4
  • Possible duplicate of [How do you reverse a string in place in C or C++?](https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – Kaldrr Sep 11 '19 at 08:33
  • 1
    @Kaldrr: This question here is the opposite of your finding. OP wants *not* in place reverse :-) – Klaus Sep 11 '19 at 08:35
  • It is useful to observe a string and its reverse always exist in the same location. It's good to answer questions on point but sometimes the better solution is preferable. – Michael Foukarakis Sep 11 '19 at 08:41
  • *reverse iterator* provided by `rbegin`, `rend` might help. – Jarod42 Sep 11 '19 at 08:42
  • 2
    Ideally you shouldn't create an new string **just to check palindrome** (unless you have some other use case). To check palindrome you can simply create two pointers/iterators from start and end and check for exact match untill both pointers reach the middle/crossover. This would save you time and space which is a treasure to a coder. – RC0993 Sep 11 '19 at 08:45

3 Answers3

5

Create the new string, by using the reverse iterators:

string reversedString(myString.rbegin(), myString.rend());

Further by using standard algorithms and the reverse iterators You can do (since C++14)

const bool is_palindrome = std::equal(myString.begin(), myString.end(),
                                      myString.rbegin(), myString.rend()));
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • 1
    Or even `auto midSize = s.size() / 2; bool res = std::equal(s.begin(), s.begin() + midSize, s.rbegin(), s.rbegin() + midSize));`. – Jarod42 Sep 11 '19 at 09:46
2

how can I save the reversed string without overwriting the old one?

std::string has both a copy constructor and a copy assignment operator making both these possible:

std::string reversedString(myString); // copy constructor
// or
std::string reversedString;
reversedString = myString;            // copy assignment

// finally
std::reverse(reversedString.begin(), reversedString.end());

And as others have pointed out, a std::string can be constructed using iterators. By constructing the new string using myStrings reverse iterators rbegin() and rend() you can create your reversed string directly without the need to call std::reverse:

std::string reversedString(myString.rbegin(), myString.rend());

For the task at hand, to find out if it's a palindrome, you don't need any of the above. Just compare the characters in myString in-place from begin() and rbegin() towards the middle of the string:

bool is_palindrome = 
    std::equal(myString.begin(), 
               std::next(myString.begin(), str.size() / 2U),
               myString.rbegin()
    );
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

Just copy the string before reversing it:

string reversedString = myString;
reverse(reversedString.begin(), reversedString.end()); // reverse is now done on the copy
Blaze
  • 16,736
  • 2
  • 25
  • 44