2014-11-16 1 views
0

문자열을 조작하기 위해 작은 코드를 작성했습니다. 첫 번째 함수에서 입력 문자열이 회문인지 확인합니다. 두 번째 함수는 주 문자열에서 하위 문자열을 제공합니다.가장 큰 서브 팔린 드롬을 찾을 수

이제는 함수에 사용하여 주 문자열에서 가장 큰 "서브 파인드rome"을 찾아야합니다. 불행하게도 나는 그것을 어떻게하는지 모른다.

하위 문자열을 생성하는 코드 샘플을 이미 찾았지만 두 함수 "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; 
} 
+0

전역 변수를 사용하지 마십시오. 매개 변수가있는 함수를 사용하십시오. 전역 변수를 제거 할 때'check_palindrome'에 필요한 매개 변수는 무엇입니까? 'check_subpalindrome' 함수를 작성하십시오. 다른 매개 변수 세트가 필요합니까? –

답변

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

간단한 해결 중첩 루프를 생성한다 : 여기서

코드이다. 외부 루프는 하위 문자열의 시작 부분을 반복해야합니다. 내부 루프는 부분 문자열의 반복 끝입니다.

그런 다음 라이브러리 함수 strncpy을 사용하여 substring이라는 또 다른 문자열을 만듭니다. 그렇다면 그것이 palindome 있는지 확인해야합니다. 이를 수행하려면 함수 check_palindrom()을 편집해야합니다. 이는 인수로 substring을 사용해야하기 때문입니다.

substring이 palindrome 인 경우 가장 큰 크기인지 확인한 후 다른 버퍼에 저장하십시오.

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. 
    } 
} 
관련 문제