Recursion: recursive determination of string-to-text phenomena

Source: Internet
Author: User
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:

  1. Recursive: recursive thinking
  2. Recursion: two conditions that must be met by recursion
  3. Recursion: recursive determination of string-to-text phenomena
  4. Recursive: recursive implementation of binary search algorithms
  5. Recursion: the efficiency of recursion
  6. Recursion: recursion and loop
  7. Let's talk about recursion: Is loop and iteration the same thing?
  8. Recursive computing and iterative computing
  9. On Recursion: Learn about tail recursion from Fibonacci
  10. Recursive: Tail recursion and CPS
  11. Recursion: add more knowledge about Continuation.
  12. Recursion: Tail recursion in PHP and its optimization
  13. Recursion: Tail recursion optimization from the perspective of Assembly

This article is available at http://www.nowamagic.net/librarys/veda/detail/2316.

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.