Recursive Determination of string retrieval Phenomena

Source: Internet
Author: User

We talked about some recursive ideas and some conceptual understandings. Here we try to solve some problems with recursion. For example, text.

A text return is a string that is the same as reading and reading. For example, level and eye are all input files. The iterative method can be used to quickly determine whether a string is a return object. How can we implement it using recursive methods?

First, we need to consider two recursive conditions: Macau casino gameplay

  • First, can this problem be resolved into the same form but smaller scale?
  • Second: if such a decomposition exists, is there a simple situation for such decomposition?

First, let's see if there is a qualified decomposition at the first point. It is easy to find that if a string is a return, there must be a smaller return within it. For example, the eve in level is also a text return. Furthermore, we note that the first character of a bounce message must be the same as the last character.

Therefore, we naturally have the following method:

First, judge whether the first and last characters of the given string are equal. If they are equal, then determine whether the string after the first and last characters are removed as a background. If they are not equal, the string is not a background.

Note: We have successfully reduced the problem. The string with the first and last characters removed is certainly smaller than the original string.

Next, let's look at the second point. Is there a simple situation for such decomposition? Simple situations are required when recursion is used. Otherwise, your recursive program may enter endless calls.

For the background problem, we can easily find that a single character string must be a background, so only one character is a simple situation, but it is not the only simple situation, because the empty string is also a return. In this way, we can see two simple situations of the Back-to-text problem: the number of characters is 1 and the number of characters is 0.

Well, both conditions are met. Based on the above analysis, we can easily compile a recursive implementation method to solve the problem of returning the text:

01 #include "stdio.h"
02 #include "string.h"
03  
04 int main(void)
05 {
06     int n, rs;
07     char str[50];
08  
09     printf("Enter the string to be retrieved :");
10     scanf("%s",&str);
11  
12     n = (int)strlen(str);
13     rs = is_palindereme(str, n);
14     printf("%d ", rs);
15 }
16  
17 int is_palindereme(char *str, int n)
18 {
19     printf("Length: %d \n",n);
20     printf("%c ----- %c\n", str[0], str[n-1]);
21     if(n == 0 || n == 1)
22         return 1;
23     else{
24         //printf("%d, %d\n", str[0], str[n-1]);
25         return ((str[0] == str[n-1]) ? is_palindereme(str+1, n-2) : 0);
26     }
27 }

The program running result is:

View Source print?
1 Enter the level string to judge the background.
2 Length: 5
3 l ----- l
4 Length: 3
5 e ----- e
6 Length: 1
7 v ----- v
8 1

Recursive Determination of string retrieval Phenomena

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.