I am using the below function and getting the below output. Can someone please explain why.
It prints out that they are equal after the for loop finishes but when compared the logic says they are not equal.
#include <iostream>
#include <string.h>
bool palindrome;
// Define is_palindrome() here:
bool is_palindrome(std::string text){
std::string backwards = "";
for (int i = text.length();i >= 0; i--){
backwards = backwards + text[i];
std::cout << backwards << std::endl;
}
std::cout << backwards << " and " << text << std::endl;
if (text == backwards){
std::cout << "they are equal" << std::endl;
palindrome = true;
}
else{
std::cout << "they are not equal" << std::endl;
palindrome = false;
}
return palindrome;
}
int main() {
std::cout << is_palindrome("madam") << "n";
}
OUTPUT
m
ma
mad
mada
madam
madam and madam
they are not equal
0
krogerson2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
13
Instead of int i = text.length()
it should be i = text.length() - 1
because you make an extra loop.
Also, I’ve changed the return state.
#include <iostream>
#include <string>
bool is_palindrome(std::string text) {
std::string backwards = "";
for (int i = text.length() - 1; i >= 0; i--) {
backwards = backwards + text[i];
}
std::cout << backwards << " and " << text << std::endl;
if (text == backwards) {
std::cout << "they are equal" << std::endl;
return true;
} else {
std::cout << "they are not equal" << std::endl;
return false;
}
}
int main() {
std::cout << is_palindrome("madam") << "n";
}
2
It can be implemented easier. The easier code usually has less errors and is easy to debug.
bool is_palindrome(std::string text) {
return text == std::string(text.rbegin(), text.rend());
}
Even better with inclusion <algorithm>
library and not creating a temporary string object
bool is_palindrome(std::string text) {
return std::equal(text.begin(), text.end(), text.rbegin());
}
Optimized for less CPU consumption time, but a longer code (taking into account that the first half matches the reversed second half in a palindrome):
bool is_palindrome(std::string text) {
return std::equal(text.begin(), text.begin() + (text.size() + 1) / 2,
text.rbegin());
}
You issue in the question is int i = text.length()
and access out of string bounds, undefined behavior. In particular practice you get "madam"
in backwards
, the unprintable first char.
5
#include<iostream>
#incldue<string>
bool is_palindrome(std::string text) {
int length = text.length();
for (int i = 0; i < length / 2; i++) {
if (text[i] != text[length - i - 1]) {
return false;
}
}
return true;
}
int main() {
std::string word = "madam";
if (is_palindrome(word)) {
std::cout << word << " is a palindromen";
} else {
std::cout << word << " is not a palindromen";
}
return 0;
}
I think you are taking string indexes wrong. The above maybe corrected and optimized solution.
1
You can use a forward and backward iterator to compare the characters front/back to middle like this too.
And better you can test correctness at compile time
#include <string>
#include <string_view>
constexpr bool is_palindrome(std::string_view sv)
{
auto front = sv.begin();
auto back = sv.rbegin();
for (;front < back.base(); ++front,++back)
{
if (*front != *back) return false;
}
return true;
}
int main()
{
static_assert(is_palindrome("a"));
static_assert(is_palindrome("aaa"));
static_assert(is_palindrome("tacocat"));
static_assert(!is_palindrome("ab"));
}