Palindrome checker fails all but 1 hidden use case | Sololearn: Learn to code for FREE!

+1

Palindrome checker fails all but 1 hidden use case

So I’m very new coding, but I wanted to tackle this code without looking up any solutions & only extrapolating from lessons that I learned in solo up to this point so I took a pretty “creative” rout. This was before I figured out you could reverse the number & check if it equals the original lol. basically my code takes an input, finds an array length & assigns it to a place in the array, it skips the middle digit & begins to compare inside to out. This fails 1/6 of checks & I want to know why. Code in com.

5/29/2022 7:10:31 AM

Madison Bratescu

26 Answers

New Answer

+3

If we are talking about why your code in the very first answer in this thread fails, then here is a thorough analysis. Be sure to read on a large screen. https://code.sololearn.com/cBni5Gnm2N2r

+2

Mad You answered your question yourself, If you enter 001 on your code , Then it would print - 1 is a palindrome, But actually, It's 001 and not 1. And 001 isn't palindrome.

+2

Mad, I think I misunderstood you. I thought _my_ correction passes only 5 out of 6. But you meant your initial solution, right? Because you wrote yours fails all but one. Last message was it passes all but one. So, where are we now?

+1

Ulysses Apokin Your code also fails, If i enter 001 in your code,It's printing 1 is palindrome,But actually it's 001.

+1

Mad By the way ,You were doing alot in order to just find a length of the a number, You can just convert it into string by to string method,And find its length via .length() method. like that- int n=22; string str=n.length(); cout<<str;

+1

#include <iostream> using namespace std; bool isPalindrome(int n) { int x = n, c = 0; int b = n; while(x != 0) { ++c; x /= 10; } int te = (c-1)/2; int v = c; int arrSize =c; int palNArray[c]; while(b != 0) { palNArray[--c] = b % 10; b /= 10; } for(int y = 0; y < v; y++); int up = te; int down = te; int m = te; if (v % 2 == 0){ ++up; } for(;m >= 0; m--) { if (palNArray[up] == palNArray[down]) { --down; up++; } else { return false; } } return true; } int main() { int n; cin >>n; isPalindrome(n); if (n <= 9){ cout <<n<<" is a palindrome"; } else{ if (isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; }} return 0; }

+1

At best, your code would have shown a non-palindromic number accidentally correct. There is too much going wrong, including the fact that you never return true from your isPalindrom function. I fixed some index miscalculations and the digit comparison loop. I have, however, not fixed any other style, logic, or CPP no-go issues, as I think, getting it working as close to your original approach is the best for starters.

+1

There may be some edge cases, Mad, that do not work. I was just fixing the part of your function that did not work, but did not check but a few examples...

+1

I am stumped at the moment. I have tested against a supposedly working solution (I cannot check myself, I haven't yet gotten this far), and for the first 4000000 numbers it works fine. It does not for negative numbers, but I don't think they are used, because the supposedly correct solution doesn't work for negative numbers either, and there is no clear definition of when a negative number is palindrome. Leading zeroes is not the issue as Ulysses Apokin already mentioned. The only thing I can think of now is the many repeated loops that you have in your code. If the number gets large enough, maybe there is a time issue? But there are at most what 9 digits? I don't really see this as a possible problem... Then perhaps warning messages in the output interfere with the code coach? But why in only one case? I don't know... Maybe some of you have other suggestions?

+1

Ani Jona 🕊 it is driving me nuts, I bet it something extremly simple I am overlooking.

+1

Ani Jona 🕊 I said it fails 1/6 as in 5 out of 6 pass.

+1

Ani Jona 🕊 I think I figured it out, 99991 is a palindrome according to my code, when it is an odd number of numbers and they are the same for the first 4 digits but the last one is different it breaks the code.

0

#include <iostream> using namespace std; bool isPalindrome(int n) { int x = n, c = 0; int b = n; while(x != 0) { ++c; x /= 10; } int te = c/2; int v = c; int arrSize =c; int palNArray[c]; while(b != 0) { palNArray[--c] = b % 10; b /= 10; } for(int y = 0; y < v; y++){ } int up = te; int down = te; int m = te; --down; if (te % 2 != 0){ ++up; } if (palNArray[up] == palNArray[down]){ for(;m > 0; m--){ --down; up++; } } else { return 0; } } int main() { int n; cin >>n; isPalindrome(n); if (n <= 9){ cout <<n<<" is a palindrome"; } else{ if (isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; }} return 0; }

0

Im extremely new, so I know it’s a mess and Im not doing this the most efficient way but I want to know how I can make this work just to see why it didn’t work. Any input on working with this solution without starting over and using the reverse method would be just for fun.

0

bool isPalindrome(int x) { int temp = x, reverse = 0; while (temp != 0) { reverse = reverse * 10; reverse = reverse + temp % 10; temp = temp / 10; } if (x == reverse) return 1; else return 0; }

0

Ulysses Apokin I know this exists, and I looked it up in frustration after my code failed and found it. I just want to know why mine might fail. I think it might not be able to accept values like 0001. ,

0

Manav Roy yes, I removed the check and I discovered that if the code were to be inputting something starting with a 0 it could be the reason it fails because it removes the 0.

0

Manav Roy Yes, I tried this without looking anything up lol. I kept going with it to see if I could make it work and got super close. After searching for solutions I felt a little silly. Figuring out why it doesnt work now is just for fun

0

I appreciate your help and input, I think I’ll put this one off to the side as a reminder of what not to do.

0

Manav Roy No, this code works correctly. As far as I know, in this task you need to find palindrome numbers. However, there is no such number as 0001, there is the number 1. So everything works correctly. If they were looking for palindrome strings, then my code would not work correctly.