Two ways to flip a string (1: Take advantage of extra Space 2: Swap)

Source: Internet
Author: User

#include <iostream>using namespace std; #define Strlenth 100char *str_reverse1 (char *str) {char temp[strlenth];// Character array: character char *p = str after storing reverse order;      Character pointer: Point to the last non ' 0;/* ' character int i = Find the last non-' + ' character and make P point to it */while (*p! = ') {P++;//while loop after P points to ' p--' and points to the last character of the array (not ' + ') }p--;/* the character after the reverse order is stored in temporary space temp */while (P! = str) {Temp[i] = *p;i++;p--;} Temp[i] = *p;    The first character after the while loop is not stored temp[i+1] = ' + ';//array at the end of f ' * * * * To copy the characters after the reverse order to the original array */strcpy (STR,TEMP); return str;} Char *str_reverse2 (char *str) {char *p = str;  Character pointer: Point to the last non-' *ret ' character char = str;//character pointer: Used to return the first address of the array */To find the last non-' s ' character and make P point to it */while (*p! = ') {p++;} p--;/* symmetric character Exchange----> reach the character rollover */char tmp;while (str < p) {tmp  = *str;*str = *p;*p   = tmp;str++;p--;} return ret;} int main () {char str[] = "Abcdefghij"; Cout<<str_reverse2 (str) <<endl;return 0;}

Two ways to flip a string (1: Take advantage of extra Space 2: Swap)

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.