简体   繁体   中英

find biggest subpalindrome in c

I have written a small piece of code to manipulate strings. In the first function I am checking if the input string is a palindrome. The second function gives a substring from the main string.

Now I have to use these to functions to find the biggest "subpalindrome" in the main string. Unfortunately I have no idea how to do that.

I have already found some code samples which generate substrings but they have not used my two functions "check_palindrome" and "substr". Some tips or a small code sample would be greatly appreciated.

Here is the code:

#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;
}

The simplest solution is to create nested loop. Outer loop must iterate the beginning of substring. Inner loop is iterating ending of substring.

Then we create another string called substring , using library function strncpy . Then you should check if it is palindome. To do it you must edit your function check_palindrom() , because it should take substring as argument.

If the substring is palindrome you check if it has biggest size and if it is you save it into another buffer.

substring[100];
for (char *begin = text; begin < text + strlen(text); begin++) {
    for (char *end = begin; end <= text + strlen(text); end++) {
        strncpy(substring, begin, end - begin + 1); //creating substring
        //... here we must check if substring is palindome
        //if it is we check if it has the biggest size. If yes then save it.
    }
}
#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;
}

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