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? If a string is a background, there must be a smaller background 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.
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:
- 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:
# Include "stdio. h "# include" string. h "int main (void) {int n, rs; char str [50]; printf (" Enter the string to be retrieved: "); scanf (" % s ", & str); n = (int) strlen (str); rs = is_palindereme (str, n); printf ("% d", rs );} int is_palindereme (char * str, int n) {printf ("Length: % d \ n", n); printf ("% c ----- % c \ n ", str [0], str [n-1]); if (n = 0 | n = 1) return 1; else {// printf ("% d, % d \ n ", str [0], str [n-1]); return (str [0] = str [n -1])? Is_palindereme (str + 1, n-2): 0 );}}
The program running result is:
Enter the string to be retrieved: levelLength: 5l ----- lLength: 3e ----- eLength: 1 v ----- v1
Additional reading
The topic list of this article is as follows:
- Recursive: recursive thinking
- Recursion: two conditions that must be met by recursion
- Recursion: recursive determination of string-to-text phenomena
- Recursive: recursive implementation of binary search algorithms
- Recursion: the efficiency of recursion
- Recursion: recursion and loop
- Let's talk about recursion: Is loop and iteration the same thing?
- Recursive computing and iterative computing
- On Recursion: Learn about tail recursion from Fibonacci
- Recursive: Tail recursion and CPS
- Recursion: add more knowledge about Continuation.
- Recursion: Tail recursion in PHP and its optimization
- Recursion: Tail recursion optimization from the perspective of Assembly
This article is available at http://www.nowamagic.net/librarys/veda/detail/2316.