A typical strrev string inversion function using C language

Source: Internet
Author: User

The string inversion function strrev is not a standard library function in C language. Many C language compilers do not provide support for it. For example, if you enter the shell command MAN 3 strlen in Linux, the screen will display,

STRLEN(3)                  Linux Programmer's Manual                 STRLEN(3)NAME       strlen - calculate the length of a stringSYNOPSIS       #include <string.h>       size_t strlen(const char *s);DESCRIPTION       The  strlen() function calculates the length of the string s, excluding       the terminating null byte ('\0').RETURN VALUE       The strlen() function returns the number of characters in s.CONFORMING TO       SVr4, 4.3BSD, C89, C99.SEE ALSO       string(3), strnlen(3), wcslen(3), wcsnlen(3)COLOPHON       This page is part of release 3.35 of the Linux  man-pages  project.   A       description  of  the project, and information about reporting bugs, can       be found at http://man7.org/linux/man-pages/.GNU                               2011-09-28                         STRLEN(3)

It tells you that the function implemented by strlen is calculate the length of a string, and # include <string. h> is required for reference. If you enter the man 3 strrev command, shell will tell you,

There are no entries on the strrev manual page in section 3rd.

Through the above operations, it is also verified that GCC does not provide support for strrev. Because it is a standard function, the compiler does not support it for a reason.

Strrev functions are not commonly used, but they are still available for digital conversion and encryption, because some compilers and VC for embedded platforms provide support for them. For compilers that do not support strrev functions, many netizens have provided a lot of valuable solutions. However, the source code provided by VC is extracted directly, which seems to be more efficient and portable, the following provides a classic implementation code:

Char * strrev (char * s) {/* H points to the S header */char * H = s; char * t = s; char ch; /* t points to the end of S */while (* t ++) {}; t --;/* offsets against T ++ */t --; /* skip the terminator '\ 0' * // * when h and T are not duplicated, exchange the characters they point to */while (H <t) {CH = * h; * H ++ = * t;/* H moves toward the end */* t -- = CH; /* t move to the header */} return (s );}

Related Article

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.