checking if the inputs are valid Palindrome

How can I make this program work to determine whether these 5 palindromes are valid or not?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>

struct Result {
	int {};

	int palindrome1 = {10201};
    int palindrome2 = {1003003001};
    int palindrome3 = {88};
    int palindrome4 = {0};
	int palindrome5 = {202};

    std::ostream& operator<<(std::ostream& os, const Result& j) {
	return os << j.palindrome1 << ", ";

}

template <typename T>
class Palindrome {
private:
	T* q_array {};
	size_t q_capacity {}, q_size {}, q_front {}, q_back {};

public:
	Palindrome(size_t newCapacity);
	bool isEmpty() const;
	bool isFull() const;
	size_t qSize() const;
	void displayFront() const;
	void displayBack() const;
	void enqueue(T newData);
	void dequeue();
	void clear();
	void displayAll() const;

	~Palindrome() {
		delete[] q_array;
	}

	Palindrome(const Palindrome&) = delete;
	Palindrome& operator=(const Palindrome&) = delete;
};

template <typename T>
Palindrome<T>::Palindrome(size_t newCapacity) : q_capacity { newCapacity }, q_array { new T[newCapacity] } {}

template <typename T>
bool Palindrome<T>::isEmpty() const {
	return q_size == 0;
}

template <typename T>
bool Palindrome<T>::isFull() const {
	return q_size == q_capacity;
}

template <typename T>
size_t Palindrome<T>::qSize() const {
	return q_size;
}

template <typename T>
void Palindrome<T>::displayFront() const {
	if (isEmpty())
		std::cout << "Queue is empty\n";
	else
		std::cout << "Front: " << q_array[q_front] << '\n';
}

template <typename T>
void Palindrome<T>::displayBack() const {
	if (isEmpty())
		std::cout << "Queue is empty\n";
	else
		std::cout << "Back: " << q_array[q_back ? (q_back - 1) : q_capacity - 1] << '\n';
}

template <typename T>
void Palindrome<T>::clear() {
	q_size = 0;
	q_front = 0;
	q_back = 0;
}

template <typename T>
void Palindrome<T>::enqueue(T newData) {
	if (isFull())
		std::cout << "Queue is Full.\n";
	else {
		q_array[q_back] = newData;
		q_back = (q_back + 1) % q_capacity;
		++q_size;

	}
}

template<typename T>
void Palindrome<T>::dequeue() {
	if (isEmpty())
		std::cout << "Queue is Empty.\n";
	else {
		const auto temp { q_array[q_front] };

		q_front = (q_front + 1) % q_capacity;
		--q_size;

	}
}

template<typename T>
void Palindrome<T>::displayAll() const {
	std::cout << "Printing queue\n";
	for (size_t i { q_front }, c {}; c < q_size; i = (i + 1) % q_capacity, ++c)
		std::cout << q_array[i] << '\n';

	std::cout << '\n';
}

int main() {
    
    //EXAMPLE OF PRINTING THE OUTPUTS.
	Palindrome<Result> ResultQ(10);

	ResultQ.enqueue({10201, "This is a valid Palindrome"});
	ResultQ.enqueue({1003003001, "This is not a valid Palindrome"});
	ResultQ.displayAll();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <concepts>
#include <type_traits>
#include <ranges>
#include <algorithm>
#include <string>
#include <list>

template < std::unsigned_integral INT > INT reverse( INT n, int rev_so_far = 0 )
{
    if( n == 0 ) return rev_so_far ;
    rev_so_far *= 10 ;
    rev_so_far += n%10 ;
    return reverse( n/10, rev_so_far ) ;
}

template < std::unsigned_integral INT >
bool is_palindrome( INT n  ) { return n == reverse(n) ; }

template < std::signed_integral INT >
bool is_palindrome( INT n  ) { return n < 0 ? false : is_palindrome( std::make_unsigned_t<INT>(n) ) ; }

template < std::ranges::random_access_range RANGE >
bool is_palindrome( const RANGE& range )
{
    const auto n = std::ranges::size(range) / 2 ;
    return std::equal( std::ranges::begin(range), std::ranges::begin(range) + n, std::ranges::rbegin(range) ) ;
}

template < std::ranges::bidirectional_range RANGE >
bool is_palindrome( const RANGE& range )
{
    return std::equal( std::ranges::begin(range), std::ranges::end(range), std::ranges::rbegin(range) ) ;
}

// usage:
int main()
{
    const long array[] { 1, 2, 3, 4, 4, 3, 2, 1 } ;
    std::cout << std::boolalpha << is_palindrome( 12344321 ) << '\n'
               << is_palindrome(array) << '\n'
               << is_palindrome( std::string("123454321") ) << '\n'
               << is_palindrome( std::list<std::string> { "abc", "def", "ghi", "def", "abc" } ) << '\n' ;
}

https://coliru.stacked-crooked.com/a/c6bc6573bcd5e07a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

bool isPalindrome( unsigned long long N )
{
   unsigned long long pval = 1, qval = 1, temp = N / 10;
   while ( temp )
   {
      pval *= 10;
      temp /= 10;
   }
   for ( ; pval > qval; pval /= 10, qval *= 10 ) if ( ( N / qval - N / pval ) % 10 ) return false;
   return true;
}


int main()
{
   unsigned long long tests[] = { 10201, 1003003001, 88, 0, 202, 5, 50, 123, 4321, 12345432, 12345678987654321 };
   for ( auto N : tests ) cout << N << ( isPalindrome( N ) ? " is " : " is not " ) << "a palindrome\n";
}

Last edited on
@vonits23 - Have you been told to use this self-coded queue for this exercise? If not, why are you trying to solve this way - there's much simpler ways of doing it (eg see lastchance's code above).

Last edited on
Another code which works with an input :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
  
int main()
{
    int inputNumber, reverseNumber = 0, rightDigit, temp;
    std::cout << "Enter an integer : ";
    std::cin >> inputNumber;
    temp = inputNumber;
    
    do {
        rightDigit = temp % 10;
        reverseNumber = (reverseNumber * 10) + rightDigit;
        temp = temp/10;
    }while(temp != 0);
    
    reverseNumber == inputNumber ? std::cout << inputNumber << " is Palindrome Number"
                                 : std::cout << inputNumber << " is not Palindrome Number";
    
    return 0;
}
If you have to use a queue, then consider (using std::queue):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <queue>

using P = unsigned long long;

bool isPalindrome(P N) {
	const auto pwrs { [](P N) {
		P pval {1};

		for (auto temp { N / 10 }; temp; pval *= 10, temp /= 10);
		return pval;} };

	std::queue<P> digits;

	for (auto qval { N }, pval{pwrs(N)}; pval; digits.push(qval / pval), qval %= pval, pval /= 10);
	for (size_t i { digits.size() / 2 }; i--; digits.pop(), N /=10)
		if (digits.front() != N % 10)
			return false;

	return true;
}

int main() {
	const P tests[] { 10201, 1003003001, 88, 0, 202, 5, 50, 123, 4321, 12345432, 12345678987654321 };

	for (auto N : tests)
		std::cout << N << (isPalindrome(N) ? " is " : " is not ") << "a palindrome\n";
}



10201 is a palindrome
1003003001 is a palindrome
88 is a palindrome
0 is a palindrome
202 is a palindrome
5 is a palindrome
50 is not a palindrome
123 is not a palindrome
4321 is not a palindrome
12345432 is not a palindrome
12345678987654321 is a palindrome

thank you guys :)
Topic archived. No new replies allowed.