0

I need help with this project, C++ functions.

#include <iostream> #include <string> using namespace std; bool isPalindrome(int x) { string palindrome; bool p =false; palindrome = (string) x; for(int i = 0; i < palindrome.length(); i++) { for(int j = palindrome.length(); j >=0; j--) { if((palindrome[i] == palindrome[j]) && i != palindrome.length() && j != 0) { continue; } if(i == palindrome.length() && j == 0) { return p = true; } else { return p; } } } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

22nd Nov 2022, 5:33 PM
Dianah Delisile
Dianah Delisile - avatar
1 Réponse
+ 4
You can convert a integer to string by to_string( number) ; Why you need 2 loops? That checks each character from start with all characters from the last to first.. You need only check first with last (ie length()-1 ) , then second with length()-2, ... So you have to do i++ at the same time j-- ; with a single loop. If match, continue else break loop. Start from j = length()-1; return result after loop..
22nd Nov 2022, 6:10 PM
Jayakrishna 🇮🇳