Reverse_string (char *string) recursively implements string rollover

Source: Internet
Author: User

See an example before a function is implemented

void Fun (int i) {if (i > 0) Fun (I/2);p rintf ("%d", I);} int main (void) {fun (ten); return 0;}

What is the output of the result?



This is an example of the "C language in depth" in this example printf ("%d", I); The statement is a part of the fun function that must execute once the fun function is printed once. The function expansion process is as follows:


void Fun (int i) {//fun (I/2), if (i > 0) {if (I/2 > 0) {if (I/4 > 0) {...} printf ("%d", I/4);} printf ("%d", I/2);} printf ("%d", I);}

Is it a lot clearer?


The same idea to complete the reverse_string (char *string) function code is as follows:


/* Write a function reverse_string (char * string) (Recursive implementation) */#include <stdio.h> #include <math.h>void reverse_string ( Char *string) {if (* (++string) = ' string-1 ') reverse_string (string);p rintf ("%c", * ());} int main () {char *a = "ABCDE"; reverse_string (a);p rintf ("\ n"); return 0;}




Reverse_string (char *string) recursively implements string rollover

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.