0
Why is the output of my code for checking palindrome wrong for some of the tests in the practice question?
This is the code: #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int n, rev = 0, digit; do { digit = n % 10; rev = (rev*10) + digit; n/= 10; } while (n != 0); if (n == rev) return true; else 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; } https://code.sololearn.com/c4FAzrOSEzk7/?ref=app
18 Antworten
+ 3
I read
bool isPalindrome(int x) { ... }
And inside that function you never use that 'x'.
+ 4
Where do you ever use the parameter 'x' in your function?
+ 3
Ademola Akinsemoyin Uthman I would prefer to use arrays (work with all programming languages.)
There are two possibilities;
The string is odd in length (say 5), Check indexes (0,4) (1,3) and (2,2). If they are the same, strings are equal else NO. eg) MALAYALAM is a palindrome.
+ 1
Create a new cpp project in the playground, copy and paste your code and try some values and you will see the results.
+ 1
Include in your question a link to your code in Code Playground
+ 1
Emerson Prado I've done that.
+ 1
I think Ani Jona 🕊 means that you inside your function you never used x. So that your given value(n) will never be used.
If you print n inside your loop you will see what happens.
+ 1
Okey this I what I mean.
https://code.sololearn.com/cc797N3PLHJI/?ref=app
0
My code is running correctly in the playground but not in the code challenge.
0
Did you tested some inputs for both results?
Like
123321
12321
123123
I tested your code and everything results with a "is a palindrome" 😉
0
Okay, I edited the code and it's running for some numbers and not running for some.
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
//complete the function
int rev = 0, digit, n;
// num = n;
do {
digit = n % 10;
rev = (rev*10) + digit;
n/= 10;
} while (n != 0);
return n == rev;
}
int main() {
int n;
cin >>n;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}
A number like 8888 will have a reserve of 888, which obviously results in 'NOT a palindrome'
What did I get it wrong please?
0
Ani Jona 🕊 It's been replaced by 'int n' (actual parameter) when I called the isPalindrome () function.
0
I replaced x with n. That's why I have isPalindrome(n) in the main function.
0
Wow! Thanks a lot. It worked!
0
n value should be strored some where before comparing with rev
0
An alternative solution:
Convert the int to a string. Then, compare every string character to the "mirrored" one - first to last, second to second last, and so on. Go until you either find two different chars or reach the middle of the string. Return a boolean depending on which exit condition you found.
Seemed fun for me.
0
Emerson Prado Can a make a loop to automatically compare the characters without having to do that manually? So I can just input any word and the code runs automatically.
Can you share a link to your code pls?
0
I don't understand what you mean by "automatically" and "manually" in the loop. Could you pls elaborate?
My code:
https://code.sololearn.com/cPdk1bUrEsGN