JBGriffin
JBGriffin

Reputation: 23

Longest Substring Palindrome issue

I feel like I've got it almost down, but for some reason my second test is coming up with a shorter palindrome instead of the longest one. I've marked where I feel the error may be coming from, but at this point I'm kind of at a loss. Any direction would be appreciated!

#include <stdio.h>
#include <string.h>

/*
 *  Checks whether the characters from position first to position last of the string str form a palindrome.
 *  If it is palindrome it returns 1.  Otherwise it returns 0.
 */
int isPalindrome(int first, int last, char *str)
{
    int i;

    for(i = first; i <= last; i++){
        if(str[i] != str[last-i]){
            return 0;
        }
    }
    return 1;
}

/*
 *  Find and print the largest palindrome found in the string str.  Uses isPalindrome as a helper function.
 */
void largestPalindrome(char *str)
{
    int i, last, pStart, pEnd;
    pStart = 0;
    pEnd = 0;
    int result;

    for(i = 0; i < strlen(str); i++){
        for(last = strlen(str); last >= i; last--){
            result = isPalindrome(i, last, str);
            //Possible error area
            if(result == 1 && ((last-i)>(pEnd-pStart))){
                pStart = i;
                pEnd = last;
            }
        }
    }
    printf("Largest palindrome: ");
    for(i = pStart; i <= pEnd; i++)
        printf("%c", str[i]);
    return;
}

/*
 *  Do not modify this code.
 */
int main(void)
{
    int i = 0;
    /* you can change these strings to other test cases but please change them back before submitting your code */
    //str1 working correctly
    char *str1 = "ABCBACDCBAAB";
    char *str2 = "ABCBAHELLOHOWRACECARAREYOUIAMAIDOINEVERODDOREVENNGGOOD";

    /* test easy example */
    printf("Test String 1: %s\n",str1);
    largestPalindrome(str1);

    /* test hard example */
    printf("\nTest String 2: %s\n",str2);
    largestPalindrome(str2);

    return 0;
}

Upvotes: 1

Views: 117

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

Your code in isPalindrome doesn't work properly unless first is 0.

Consider isPalindrome(6, 10, "abcdefghhgX"):

  • i = 6;
  • last - i = 4;
  • comparing str[i] (aka str[6] aka 'g') with str[last-i] (aka str[4] aka 'e') is comparing data outside the range that is supposed to be under consideration.
  • It should be comparing with str[10] (or perhaps str[9] — depending on whether last is the index of the final character or one beyond the final character).

You need to revisit that code. Note, too, that your code will test each pair of characters twice where once is sufficient. I'd probably use two index variables, i and j, set to first and last. The loop would increment i and decrement j, and only continue while i is less than j.

for (int i = first, j = last; i < j; i++, j--)
{
     if (str[i] != str[j])
         return 0;
}
return 1;

Upvotes: 2

dbush
dbush

Reputation: 223689

Here's your problem:

for(i = first; i <= last; i++){
    if(str[i] != str[last-i]){
        return 0;
    }
}

Should be:

for(i = first; i <= last; i++, last--){
    if(str[i] != str[last]){
        return 0;
    }
}

Also, this:

for(last = strlen(str); last >= i; last--){

Should be:

for(last = strlen(str) - 1; last >= i; last--){

Upvotes: 0

tsandy
tsandy

Reputation: 931

In isPalindrome, replace the line if(str[i] != str[last-i]){ with if(str[i] != str[first+last-i]){.

Upvotes: 1

Related Questions