Two interesting recursive applets

Source: Internet
Author: User
Two recent interviews and pen-based questions are interesting:
1. Implement the library function strlen. You cannot use any library function or define any variable.
2. Implement the strrev function. This function has two parameters. Put one character string in reverse order to another character string pointer. Similarly, you cannot use any library function or define any variable.

Recursive Implementation can meet the requirements of these two questions:

  1. Void strrev (char ** DEST, const char * SRC)
  2. {
  3. If (* src = '/0 ')
  4. Return;
  5. Else
  6. {
  7. Strrev (DEST, SRC + 1 );
  8. * (* DEST) ++ = * SRC;
  9. }
  10. }
  11. Int strlens (const char * SRC)
  12. {
  13. If (* SRC! = '/0 ')
  14. Return strlens (++ SRC) + 1;
  15. Else
  16. Return 0;
  17. }

The strrev function requires both parameters to be of the char * type, but I did not expect a solution, but Char ** can be used, but this changes the pointer value of the real parameter of the target string, add '/0' at the end '.
Of course, the writing efficiency of these two functions is not necessarily high, but the recursive method is indeed quite interesting.

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.