+ 1

Help in debugging [Solved]

#include <iostream> #include <math.h> #include <algorithm> using namespace std; int main() { string str,new_str ; cin>>str; new_str = reverse(str.begin(), str.end()); if (new_str == str) { cout << str << " is a palindrome." } else { cout << str << " is not a palindrome." } return 0; } Showing Error in "line 8"

20th Dec 2020, 11:18 AM
Nikunj Vashishtha
Nikunj Vashishtha - avatar
1 Answer
+ 3
std::reverse() reverses the sequence in-place and doesn't return anything, hence trying to assign void to "new_str" is faulty: https://en.cppreference.com/w/cpp/algorithm/reverse If you need to construct a reversed sequence while leaving the original one as is, you should have a look at std::reverse_copy() instead: https://en.cppreference.com/w/cpp/algorithm/reverse_copy Note std::reverse_copy() will require the output range to at least have the same size as the input range, seeing that "new_str" is currently empty. Using a string constructor and reverse iterators, you can create the reversed string directly without any external function: string new_str{ str.crbegin(), str.crend() };
20th Dec 2020, 11:28 AM
Shadow
Shadow - avatar