0

C++ Palindrome: I don't find a working code for the palindrome test. Please help :)

So I am stuck at the palindrome code of the C++ learning path. I tried many ways to find a working code but failed. Do you have any idea of how I should change this code to make it work? I just started to learn programming yesterday so there might be some serious and obvious mistakes in the code :) #include <iostream> using namespace std; bool isPalindrome(int x) { int i = x; int counter = 0; //finding out number of digits in x do { i = i%10; counter++; } while(i!=0); int array1[counter]; int array2[counter]; //filling array1 with digits of x for(int r=1; r<=counter; r++) { array1[counter-r] = x%10; x=x-array1[counter-r]; } //filling array2 with digits of x for(int r=0; r<counter; r++) { array2 [r] = x%10; x=x-array2[r]; } int k=0; //counting times of array1[r]=array2[r] for(int r = 0; r<counter; counter--,r++) { if(array1[r]==array2[r]) { k+=1; } } //comparing counter(number of digits of n) and k(how many times array1=array2) //if counter = k => n is a palindrome => function isPalindrome=true if(k==counter) { return 1; } else { return 0; } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

11th Jun 2021, 8:22 PM
flippo
5 Answers
+ 3
Serwas! So you've figured out the %10 part that's good. You will need to split the number into two parts, not only do you want the last digit in a variable, you need the rest of the number too. int x = 1234; int last = x % 10; // 4 int rest = x / 10; // 123 Then you can x = rest; and repeat. so your do-while loop needs some division somewhere, aswell as the first for loop. Also the second array2 is redundant, here's why: After you have split your number into digits, all you need to do is check whether it's the same from the front as when read from the back. for(int i = 0; i < counter; i++) { if (array1[i] != array1[counter - i - 1]) cout << "not a palindrome"; } cout << "palindrome!"; The way you're doing it with two arrays is fine too, just a bit too much work.
11th Jun 2021, 11:48 PM
Schindlabua
Schindlabua - avatar
+ 2
flippo , may be this can help you: ▪︎take the input string ▪︎get the length of this string ▪︎create a new empty string for the reverse version ▪︎use a for loop (string length required) and iterate through the characters of the input string by starting from the end ▪︎each character you pick with the for loop has to be added to the new string by using concatenation ▪︎after for loop is done you can compare the initial string against the new created string. if it equals, string is palindrome this may not the most efficient way, but it is doing the job.
11th Jun 2021, 8:39 PM
Lothar
Lothar - avatar
+ 2
UPDATE: So I found a working code for my problem :) was easier than expected. I just overcomplicated it. Here is my final code: #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int var=x; int rem; int sum=0; while(var!=0) { rem=var%10; var=var/10; sum=sum*10+rem; } if(sum==x) {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; }
11th Jun 2021, 11:44 PM
flippo
+ 2
flippo code to reverse a string. txt is the string: # txt is a char array int wordLen = strlen(txt); for(int i=0; i<wordLen/2; i++) { char character = txt[i]; txt[i] = txt[wordLen -i -1]; txt[wordLen -i -1] = character; } cout<<txt; # or string reversed(txt); # cout<<reversed;
12th Jun 2021, 2:14 AM
Paul K Sadler
Paul K Sadler - avatar
0
Yes, thank you, that's a good idea! But my problem is that I don't know how to create a reverse string. That's why I tried to do it with 2 arrays: one array has the parameters of the given number and the other array has the same parameters but reversed. Then I wanted to compare the parameters of both arrays and see if they are equal.
11th Jun 2021, 8:52 PM
flippo