简体   繁体   English

字符串中最长的回文及其长度

[英]Longest palindrome in a string and it's length

I have a program that is supposed to go through a string identify possible palindromes, check if it is a palindrome and then return the length from palindromelength() or -1 if it is not, and print out the longest palindrome of the string. 我有一个程序,应该通过一个字符串来识别可能的回文,检查它是否是回文,然后从palindromelength()或-1返回长度palindromelength()如果不是),并打印出字符串的最长回文。 The program is compiling but the output is wrong. 程序正在编译,但输出错误。

This is my code 这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int palindromelength(char *str, int i, int j){
    char *start = str;
    char *end  = str + j;
    while(start < end){
        if(*start != *end)
            return -1;
        start++;
        end--;
    }
    return j-i+1;
}
void ispalindrome(char *str){
    int length = 1, max = 1, start =0, i, j;
    for(i=0; i<= strlen(str) -2; i++){
        for(j= 1; j <= strlen(str)-1; j++){
            if(abs(i-j) > 1){
                length = palindromelength(str, i, j);
                if( length> max){
                    max = length;
                    start = i;
                }
            }
        }   
    }
    if(max > 1){
        printf("Largest palindrome is ");
        for( ; start <= j; start++){
            printf("%c", str[start]);
            start++;
        }
    }
    else
        printf("No palindromes in string.");
}

int main(void) {
    char a[50];
    char *a2;
    printf("Enter a string\n");
    scanf("%[^\n]", a);

    int length = strlen(a) + 1;
    a2 = (char*)malloc(length*sizeof(char));
    strcpy(a2, a);
    free (a2);

    char *a3 = &a;
    ispalindrome(a3);

    return 0;
}

I have tried the palindromelength() separately with a simple string, "aracecar". 我已经用简单的字符串“ aracecar”分别尝试了palindromelength() palindrimelength(a3, 0, 4) returns -1 so that is right, palindromelength(a3, 0, 3) returns 3 so that is right, but palindromelength(a3, 1, 7) returns -1, which is wrong. palindrimelength(a3, 0, 4)返回-1,是正确的, palindromelength(a3, 0, 3) palindrimelength(a3, 0, 4) palindromelength(a3, 0, 3)返回3,是正确的,但是palindromelength(a3, 1, 7)返回-1,这是错误的。 I double checked my function with other ones on stack overflow and it seems right, what could be the problem? 我在堆栈溢出时与其他函数仔细检查了我的函数,这似乎是正确的,可能是什么问题? As for the second function ispalindrome() is there a better way I could write that one? 至于第二个函数ispalindrome() ,有没有更好的方法可以写那个呢? It just seems kind of messy right know. 似乎有点混乱,知道了。

I am a newbie, therefore I may not have yet learned some more advanced/sophisticated variations I could take to solve this. 我是新手,因此我可能还没有学会解决此问题的一些更高级/更复杂的方法。

I presume int i is the starting index of the string and int j is the ending index. 我假设int i是字符串的开始索引,而int j是结束索引。 If that is so, have a closer look at the first line of your palindromelength() function. 如果是这样,请仔细查看palindromelength()函数的第一行。 Could the function ever start from somewhere other than index 0? 函数是否可以从索引0以外的其他地方开始?

As for ways to do the ispalindrome() function, there are many algorithms out there to check whether or not an input is a palindrome, I would suggest having a look around and finding out different methods. 至于执行ispalindrome()函数的方法,有很多算法可以检查输入是否为回文,我建议您环顾四周并找出不同的方法。 If your way works, great! 如果您的方法可行,那就太好了! probably the cleanest way is to use recursion although that can take some thinking. 也许最干净的方法是使用递归,尽管这可能需要一些思考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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