Way One: reverse the string output without changing the memory ( Recursive Implementation )
void Reverse_string (char *str) {/* * encountered ' + ' do nothing, Function end */if (*str = = ') '; else{/* output next */reverse_string (str + 1);cout< <*str;}}
Mode two:Change Memory (Exchange Method)
/* Non-recursive implementation: Operation Memory */char *reverse_string1 (char *str) {char *left = str; The leftmost char *right = str that holds the character array; Holds the rightmost while (*right! = ') of the character array {right++;} After the/*while loop, right points to ' right--', and then points to the last non-' temp;while ' character */right--;/* the two elements of the right-and-left symmetry of the Exchange */char, < right) {temp = * Left;*left = *right;*right = temp;left++;right--;} return str;}
Way Three:Change Memory (Recursive Implementation)
/* Recursive implementation: Operation memory *//* recursive every call: the string header to invert and the tail each decrease by one */char *reverse_string2 (char *str) {int lenth = strlen (str);/* Empty string lenth= 0 or lenth=1 A string with only one valid character without reversing */if (lenth <= 1) {return NULL;} char temp; /* When a string has at least two non-' characters ' characters used to invert */if (Lenth > 1) {temp = str[0];str[0] = str[lenth-1];/* The last character is no longer processed at the next recursion */str[lenth-1] = ' \ 0 '; */* recursive each call, to reverse the string head and tail each reduced by one */reverse_string2 (str + 1); Str[lenth-1] = temp;//(using the properties of the station first entry)}return str;}
Test:
#include <stdio.h> #include <iostream>using namespace std;/* recursive implementation: Operation memory *//* recursive every call: the string header to invert and the tail each decrease by one */char * Reverse_string2 (char *str) {int lenth = strlen (str);/* empty string lenth=0 or lenth=1 a string with only one valid character does not need to be reversed */if (lenth <= 1) {return NULL;} char temp; /* When a string has at least two non-' characters ' characters used to invert */if (Lenth > 1) {temp = str[0];str[0] = str[lenth-1];/* The last character is no longer processed at the next recursion */str[lenth-1] = ' \ 0 '; */* recursive each call, to reverse the string head and tail each reduced by one */reverse_string2 (str + 1); Str[lenth-1] = temp;//(using the properties of the station first entry)}return str;} /* Non-recursive implementation: Operation Memory */char *reverse_string1 (char *str) {char *left = str; The leftmost char *right = str that holds the character array; Holds the rightmost while (*right! = ') of the character array {right++;} After the/*while loop, right points to ' right--', and then points to the last non-' temp;while ' character */right--;/* the two elements of the right-and-left symmetry of the Exchange */char, < right) {temp = * Left;*left = *right;*right = temp;left++;right--;} return str;} void Reverse_string (char *str) {/* * encountered ' + ' do nothing, Function end */if (*str = = ') '; else{/* output next */reverse_string (str + 1);cout< <*str;}} int main () {char str1[] = "ABCDEFGH"; char str2[] = "ABCDEFGH"; char str3[] = "abcdefgh"; Cout<<reverse_string1 (STR1) <<endl;cout<<reverse_string2 (str2) <<endl;reverse_string ( STR3); Cout<<endl;return 0;}
Several ways to implement string inversion