- 1
Checking palindrome
Hello everyone, I need to check whether a input number is a palindrome. Here is my code, what am I doing wrong? Please help me. #include <iostream> using namespace std; bool isPalindrome(int x) { int a[x]; int b = x; int c = sizeof(a)-1; for (int d = 0; d < c; d++){ for (c; c > 0; c--){ if (a[d] == a[c]) return true; if (a[d] != a[c]) return false; } } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout << n << " is a palindrome"; } else { cout << n << " is NOT a palindrome"; } return 0; }
1 Réponse
+ 1
your isPalindrome function is totally wrong ^^
you declare an array of x int, but you did'nt assign any value to it...
you assign b with a copy of value x, but you never use it
you make two nested loop, but you only need one (and compute the opposite value)
anyway, you try to access array a values, but you never has assigned values to it, and in all case you return from the function as soon as first iteration ^^