在c中找到最长的子回文

时间:2014-11-16 14:40:42

标签: c palindrome

我写了一小段代码来操纵字符串。在第一个函数中,我正在检查输入字符串是否是回文。第二个函数从主字符串中提供子字符串。

现在我必须使用这些函数来找到主字符串中最大的“subpalindrome”。不幸的是我不知道该怎么做。

我已经找到了一些生成子字符串的代码示例,但它们没有使用我的两个函数“check_palindrome”和“substr”。我们非常感谢一些提示或小代码示例。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#define STR_MAX 6 // to define the max amout of letters in the sting

char text[STR_MAX]; //global var

int check_palindrome() {

   printf("Is '%s' a palindrome?\n", text);

   int begin, middle, end, length = 0;

   while (text[length] != '\0' )
      length++;

   end = length -1;
   middle = length/2;

   for( begin = 0 ; begin < middle ; begin++ ) {
      if ( text[begin] != text[end] ) {
         printf("False\n");
         break;
      }
      end--;
   }

   if( text[begin] == text[middle])
    printf("True\n");

   return EXIT_SUCCESS;
}


int substr() {
   int begin, end = 0;

   printf("Enter your starting point: \n");
   scanf("%d", &begin);

   printf("enter last string: \n");
   scanf("%d\n", &end);

   printf("Your substring is: \n");
   while (begin <= end) {
    printf("%c", text[begin]);  // loop for my substing from begin to end
    begin += 1;
   }
   printf("\n");
   return EXIT_SUCCESS;
}


int main(void) { 

// for function check palindrome   
   printf("Here you can proof if your input is a palindrome\nPut in a string please: ");
   fgets(text, STR_MAX, stdin); // i use fgets instead of gets
   check_palindrome();


// for function substr
   printf("Now you can choose a substring\n");
   substr();   

   return EXIT_SUCCESS;
}

2 个答案:

答案 0 :(得分:1)

最简单的解决方案是创建嵌套循环。外循环必须迭代子串的开头。内循环迭代子串的结尾。

然后我们使用库函数substring创建另一个名为strncpy的字符串。然后你应该检查它是否是palindome。为此,您必须编辑您的函数check_palindrom(),因为它应该以{{1​​}}为参数。

如果substring是回文,你检查它是否有最大尺寸,如果它是你将它保存到另一个缓冲区。

substring

答案 1 :(得分:0)

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

#define S_(x) #x
#define S(x) S_(x)

#define STR_MAX 64 // to define the max amount of letters in the string

char *biggest_subpalindrome(const char *text);

int main(void){
    char text[STR_MAX+1];

    printf("Put in a string please: ");
    scanf("%" S(STR_MAX) "s", text);

    char *subpalindrome = biggest_subpalindrome(text);
    if(subpalindrome){
        puts(subpalindrome);
        free(subpalindrome);
    } else {
        puts("NOT FIND!");
    }
    return 0;
}

bool check_palindrome(const char *begin, const char *end){
    if(begin == end)
        return false;
    while(*begin == *end){
        ++begin;
        --end;
    }
    return begin > end;
}

char *biggest_subpalindrome(const char *text){
    const char *begin = text, *end = strrchr(text, *begin);
    const char *begin_max;
    size_t max_len=0;

    while(*begin){
        while(begin < end){
            if(check_palindrome(begin, end)){
                size_t len = end - begin + 1;
                if(len > max_len){
                    max_len = len;
                    begin_max = begin;
                }
                break;
            }
            while(*--end != *begin)
                ;
        }
        ++begin;
        end = strrchr(begin, *begin);
    }
    if(max_len){
        char *ret = calloc(max_len + 1, sizeof(char));
        memcpy(ret, begin_max, max_len);
        return ret;
    }
    return NULL;
}