It might seem like long but I over explained it.
So I have my problem is not necessarily finding the palindromes it is just the finding the length of the palindrome I can't figure out and my code doesn't work for single digit palindromes (yes we count them too) so here is the code and the explanation:
#include <stdio.h>
#define LEN 9
int *lastEqual(int *p, int *q) {
int *rightmost;
int *temp;
int *zero = p;
while (p <= q) {
if (*zero == *(p + 1)) {
temp = p + 1;
if (temp < q) {
rightmost = temp;
}
}
p++;
}
return rightmost;
}
What this funtion does or suppose to do:
*For the given array: {3,6,7,8,7,6,5,3,5} If lastEqual is called by the references of the bold numbers, it returns a pointer to 3.(The one closest to the end of array)*
*For the given array: {3,6,7,8,7,6,5,3,5} If lastEqual is called by the references of the bold numbers it returns a pointer to 6.*
Here is the second function:
int isPalindromic(int *p, int *q) {
int *left = p;
int *right = q;
int pali;
while (left < right) {
if (*left == *right) {
pali = 1;
} else {
pali = 0;
}
left++;
right--;
}
return pali;
}
Here is what it does or is supposed to do:
For the given array: {3,6,7,8,7,6,5,3,5} If isPalindromic is called by the references of the bold numbers it returns 0, since the numbers between those addresses do not represent a palindrome.
For the given array: {3,6,7,8,7,6,5,3,5} If isPalindromic is called by the references of the bold numbers it returns 1, since the numbers between those addresses represent a palindrome.
And here is the main function:
int main() {
int *p, *q, *rightmost;
int arr[LEN] = { 3, 6, 7, 8, 7, 6, 5, 3, 5 };
p = arr;
q = p + (LEN - 1);
for (int i = 0; i < LEN; i++) {
if (isPalindromic(p + i, lastEqual(p + i, q)) == 1) {
rightmost = lastEqual(p + i, q);
printf("Palindrome at index %d, length %ld\n", i, &p - &rightmost);
}
}
return 0;
}
And the output should be like these but I cant figure out how to find lenght and why it doesnt count the single digit number as a palindrome like this 8 :
{3,6,7,8,7,6,5,3,5}
so 3 to 3 is not a palindrome 6 to 6 is 7 to 7 is and 8 should be counted as well because there is not a pair of 8
The output Should be like these:
Input Array: {1}
Output: “palindrome at index 0, length: 1”
Input Array: {5, 6, 7, 8, 7, 6, 5, 2, 5}
Output: “palindrome at index 0, length: 7”
Input Array: {2, 7, 6, 11, 10, 11, 6, 5, 3}
Output: “palindrome at index 2, length: 5”
Input Array: {7, 8, 9, 8, 7}
Output: “palindrome at index 0, length: 5”
Input Array: {2, 7, 4, 3, 2, 6, 1, 2, 1}
Output: “palindrome at index 6, length: 3”