简体   繁体   中英

find the longest palindrome

I want to return the longest palindrome, for example "civic" return "civic", "fawziizw" return "wziizw" This is what I tried:

int ispalindrome (char str[],int start,int end) {
    int i=0;
    int j=0;
    int counter=0;
    for(i=start; i<strlen(str); i++) {
        if (str[i]==str[end-i]){
            counter++;
        }
    }
    if(counter==i){
        return counter;
    } else 
        return 0;
}
void longestPalindrome(char str[]) {
    int len = strlen(str);
    int i=0;
    int j=0;
    int start=0;
    Int end =0;
    int counter=0;
    int check=0;
    if (strlen(str)-1==1||strlen(str)-1==2) {
        printf("%s",str);
    } else {
        for (i=0; i<len; i++) {
            counter=0;
            for(j=len-1; j>=0; j--) {
                if(str[i]==str[j]) {
                    counter= ispalindrome(str,i,j);
                    if (counter>check&&counter>0) {
                        check=counter;
                        start=i;
                        end=j;
                    }
                }
            }
        }
        for(i=start; i<=end; i++) {
            printf("%c",str[i]);
        }
    }
}

So it works with a palindrome like "civic" or "madam", but when I try with "fawziizw" it returns "f" instead of "wziizw".

That was close!

In the function ispalindrome() , you miss the final characters, since start might not be 0 , and i being from start (>0) will have end-i miss the end of the string

int ispalindrome (char str[],int start,int end) {
    int i=0;
    int j=0;
    int counter=0;
    for(i=start; i<strlen(str); i++) {
            if (str[i]==str[end-i]){ // <===== end-i not ok if start>0
                counter++;
        }}
        if(counter==i){
        return counter;}
        else
            return 0; // better to leave whenever it's not a palindrome
    }

I suggest a simpler version. We use i as a counter from 0 , since start might be >0 , in order not to miss the final characters in that case

int ispalindrome (char str[],int start,int end) {
    for(int i=0 ; i < end-start ; i++) { 
          if (str[start+i] != str[end-i]){
                return 0; // <== return 0 if not a palindrome
        }}
     return end - start + 1; 
}

Then it should work better. In the next function, you could change

for(j=len-1; j>=0; j--)

to

for(j=len-1; j>i; j--)


Just in case you're interested, a lighter version of the longestPalindrome function

 void longestPalindrome(char str[]) { int len = strlen(str); int i,j; int tempstart=0; int tempend=0; int counter; int tempcounter=0; for (i=0; i<len; i++) { for(j=len-1; j>i; j--) { // >i counter= ispalindrome(str,i,j); if (counter>=tempcounter&&counter>0) { tempcounter=counter; tempstart=i; tempend=j; } } } for(i=tempstart; i<=tempend; i++) { printf("%c",str[i]); } printf("\n"); }

which does basically the same.

I've made this one that defines a length, and checks if there any sub-strings (of the original string) of this length that is palindrome.

The length initially is equal to the length of the entire string (so, the first sub-string is actually the entire original string), and decreases by 1 at every iteration. So, the first palindrome sub-string found is the longest palindrome.

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

int ispalindrome(char *start, int length){
    int i;
    for(i=0; i < length-1-i; ++i){
        if (start[i] != start[length-1-i]){
            return 0;
        }
    }
    return 1;
}

void longestPalindrome(char str[]) {
    int lenstr, length, i, j;
    lenstr = strlen(str);
    for (length=lenstr; length > 0; --length){
        for(i=0; length+i <= lenstr; ++i){
            if(ispalindrome(str+i, length)){
                for(j=0; j<length; ++j){
                    printf("%c", str[i+j]);
                }
                return;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM