jeudi 10 septembre 2020

C++ indicate Palindrome

I'm pretty new to coding in C++ and need your help. In the code below I want to check for Parlindrome and show the reversed text. So if I run this code, it displays the right reversed words, but dont return them true if checked for parlindrome.

The console shows: madam0 ada0 ecalevol0

Why the return is always 0?

Thanks for your help!

#include <iostream>

// Define is_palindrome() here:

bool is_palindrome(std::string text) {
  std::string text_reversed;

  for(int i = text.length(); i >= 0; i-- ) {
    text_reversed += text[i];
    std::cout << text[i];
  }

  if (text_reversed == text) {
    return true;
  } else {
    return false;
  }
}

int main() {
  
  std::cout << is_palindrome("madam") << "\n";
  std::cout << is_palindrome("ada") << "\n";
  std::cout << is_palindrome("lovelace") << "\n";
}

Aucun commentaire:

Enregistrer un commentaire