#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)