0

Is it possible to solve the C++ palindrome challenge with only the course material?

Having difficulties solving the palindrome exercise in the C++ course, I researched in the Q&A section how to do it. Apparently there are two „standard“ solutions doing this: 1. Mathematically with modulo which I would have never come up with myself without looking it up. 2. By converting it to a string and using the reverse function. Unfortunately this function is never mentioned before in the course (nor any string function at all). Or am I just blind and missed that in the course?

11th May 2021, 2:42 PM
KatharinaSt
5 RĂ©ponses
+ 2
They surely didn't expect you to solve it by reversing the string (which is the better way btw), because I highly doubt a person who has just learnt about functions knows how to explore the C++ reference, let alone know about iterators. I think they expected you to solve it mathematically, which isn't too hard to come up with. It can definitely be solved by what has been taught in the course upto that point. But I can see how it can be difficult when you look at it the first. I never came up with the mathematical solution myself, even though it's such an obvious solution once you know about it.
11th May 2021, 3:19 PM
XXX
XXX - avatar
+ 1
Ofcourse, just keep coding
11th May 2021, 2:48 PM
SùñtÎsh
SùñtÎsh - avatar
+ 1
Here's a way in which you can solve this with the modulo operator: int is_palindrome(int a) { int b = 0, c = a; while (a) { b = b * 10 + a % 10; a /= 10; } return b == c; } // Hope this helps
11th May 2021, 4:10 PM
Calvin Thomas
Calvin Thomas - avatar
0
You can solve it without modulo by using integer math. bool isPalindrome(int x) { //complete the function int y = x; for(int temp = x/10; temp; temp /= 10) { y -= 10*temp; y *= 10; y += temp; } return (y == x); }
11th May 2021, 3:58 PM
Brian
Brian - avatar
0
Calvin Thomas btw the return type shoud be bool
12th May 2021, 10:00 AM
Bot
Bot - avatar