Several ways to implement string inversion

Source: Internet
Author: User

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

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.