[Careercup] 1.2 Reverse string flipping strings

Source: Internet
Author: User

1.2 Implement a function void reverse (char *str) in C or C + + which reverses a null-terminated string.

This problem lets us use C + + or C language to flip a string, not a problem, in the previous word reverse Words in a string flipped the words in the strings used in this function, compared with the problem, the problem is simple. C language version is slightly more complex than C + +, should be a string class integrates a lot of useful functions, such as to get the length of the string, the subscript directly access AH and so on, C language implementation should be aware of the first to use a while loop to find the last character, and then go to the middle. See the code below:

C++:

class Solution {public:    void reverse (string  &s) {         int01;          while (Left < right ) {            char tmp = S[left];            S[left+ +] = S[right];            S[right--] = tmp;}}     ;

C

classSolution { Public:    voidReverseChar*str) {        Char*right =str; if(str) { while(*right) + +Right ; --Right ;  while(Str <Right ) {                CharTMP = *str; *str++ = *Right ; *right--=tmp; }        }    }};

[Careercup] 1.2 Reverse string flipping strings

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.