Hello, I'm looking for a way to determine whether a number is palindromic (reads the same both ways; 9009; 101; 1001;) or not. I feel like I can do this using arrays, but I want to be as efficient as possible, and using arrays doesn't seem all that efficient. If anyone has any knowledge or hints on how to do this I would be very grateful.
I'm not sure if it's possible to do without an array, unless there's a way to read from console input backwards. What I would do is create two pointers pointing at each end of the array (one at index 0 and the other at index size-1) and compare the two while bringing them closer together. If they don't match, return false.
I've done this before by first passing the number into a std::stringstream, then just calling the .str() method on it and then just doing normal string palindrome checking.
But if you're getting input from the console, just input it into a string in the first place then you don't need to worry about the conversion.
Is there a way to grab the number of digits within an integer and store them in a variable so I can give the array the correct size?
EDIT: After a bit of searching I found a method using integer division to count the number of digits. Here is the code I came up with, but every time I run it, it crashes.
#include <iostream>
usingnamespace std;
int palindrome_test(int palindrome_check);
int main()
{
int number;
cout << "Give me an integer.\n";
cin >> number;
cout << "That number has " << palindrome_test(number) << " digits.\n";
}
int palindrome_test(int palindrome_check)
{
bool palindrome_bool;
int size;
int palindrome_number[size];
int digits;
int digit_check;
digit_check = palindrome_check;
do
{
digit_check = digit_check / 10;
digits++;
} while (digit_check >= 0);
return digits;
}
the solution ne555 gave is the one you're looking for.
his code neither complete, nor correct, but the idea was right.
1 2 3 4 5 6
bool palindrome(int x){
int t = x, m = 0;
do m = m*10 + t%10;
while(t /= 10);
return m == x;
}
is the shortest I can make it.
whether this is more efficient depends on what your input is. either method itself hardly takes any time (I assume array is faster though). if you are given an array (user input is an array) do the thing with arrays. if you're given an int, do this. conversion is the wasteful part here.
I think I've got everything else down, but the digit count is still giving me trouble. I see other solutions here, but shouldn't my first method be performing purely integer division and leaving decimals out of the question? Or am I misinterpreting it? I'm going to move on to other methods, but I'd still like to clear this up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
int number;
int digits_a;
cout << "Give me an integer.\n";
cin >> number;
do
{
number = number / 10;
digits_a++;
} while (number != 0);
cout << "That number has " << digits_a << " digits.\n";
}
I'm almost done, but I keep getting 2 errors when trying to use the function which checks if the number is a palindrome.
The 2 errors both occur at line 28:
In function 'int main()': error: invalid conversion from 'int' to 'int*' (Not even using any pointers?) error: initializing argument 1 of 'bool palindrome_test(int*, int)'
||=== Build finished: 2 errors, 0 warnings ===|
#include <iostream>
usingnamespace std;
bool palindrome_test(int isPalindrome[], int size);
int digit_count(int x);
int main()
{
int number;
int length = 0;
int palindrome[length];
bool Palindrome_check;
cout << "Please input an integer.\n";
cin >> number;
length = digit_count(number) - 1;
palindrome[length] = number;
Palindrome_check = palindrome_test(palindrome[length],length);
if (Palindrome_check = true)
{
cout << "That number is a palindrome\n";
}
else
{
cout << "That number is not a palindrome\n;";
}
}
int digit_count(int x)
{
int digits = 0;
do
{
x /= 10;
digits++;
} while (x != 0);
return digits;
}
bool palindrome_test(int isPalindrome[], int size)
{
int f = 0; //first number
int l = size; //last number
int count;
for (count = 0; count <= size; count++)
{
if (f == l)
{
f++;
l--;
}
else
{
returnfalse;
}
}
returntrue;
}
The method ne555 and hamsterman posted works really well, and is super efficient. However, since I had already planned everything out using arrays I would like to finish it that way.
Here's the working result using ne555's and hamsterman's method:
#include <iostream>
usingnamespace std;
bool palindrome(int x);
int main()
{
int number;
bool Palindrome_check;
cout << "Please input an integer.\n";
cin >> number;
Palindrome_check = palindrome(number);
if (Palindrome_check == true)
{
cout << "That number is a palindrome\n";
}
else
{
cout << "That number is not a palindrome\n";
}
}
bool palindrome(int x)
{
int t = x, m = 0;
do
{
m = m*10 + t%10;
} while(t /= 10);
return m == x;
}