2

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”
chqrlie
  • 131,814
  • 10
  • 121
  • 189
hohenpaid
  • 99
  • 9
  • 1
    Time to talk to the duck... See [**How to debug small programs**](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – David C. Rankin Apr 18 '20 at 20:46
  • 1
    It doesn't look like there's much of a start here. I suggest focusing on getting `isPalindromic` to work first, and forget about the other function for now. Maybe even remove it from the post. `isPalindromic` is an extremely simple function and you should be able to write it and debug it without much difficulty. Once it's working solidly, *then* tackle `lastEqual`. – Tom Karzes Apr 18 '20 at 20:49
  • isPalindromic works when I test it actually but when I call it in the main it skips 8 thats all otherwise both functions work correctly ı blieve – hohenpaid Apr 18 '20 at 21:09

1 Answers1

2

Here is a simpler version of isPalindromic with the same arguments, and a much simpler version of main() to find all palindromes:

#include <stdio.h>

int isPalindromic(int *p, int *q) {
    while (p < q) {
        if (*p++ != *q--)
            return 0;
    }
    return 1;
}

int main() {
    int arr[] = { 3, 6, 7, 8, 7, 6, 5, 3, 5 };
    int len = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < len; i++) {
        for (int j = i; j < len; j++) {
            if (isPalindromic(arr + i, arr + j))
                printf("Palindrome at index %d, length %d\n", i, j - i + 1);
        }
    }
    return 0;
}

If you just want to output the longest palindrome, change the main function to this:

int main() {
    int arr[] = { 3, 6, 7, 8, 7, 6, 5, 3, 5 };
    int len = sizeof(arr) / sizeof(arr[0]);
    int max_pos = 0, max_len = 1;

    for (int i = 0; i < len; i++) {
        for (int j = i + max_len; j < len; j++) {
            if (isPalindromic(arr + i, arr + j)) {
                max_pos = i;
                max_len = j - i + 1;
            }
        }
    }
    printf("Longest palindrome at index %d, length %d\n", max_pos, max_len);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I tried it and it gave me wrong outputs when I tried it with your palindromic function – hohenpaid Apr 18 '20 at 21:14
  • but when I tried it with only the main and my functions it kind of works but the first output is this `Palindrome at index 0, length 7` This should not exist because between the 3's there is 5 which breaks Palindrome – hohenpaid Apr 18 '20 at 21:17
  • @AliUlaşHayır: sorry about that. I updated the answer with fixes in `isPalindrome` and `main` – chqrlie Apr 18 '20 at 21:27