C ++ reverse string (original)

Source: Internet
Author: User

// Different methods are used to reverse the input string, which can be used to review and consolidate the basic knowledge of C ++.

/* Analysis process:

If you want to make the passed string a constant const string, this operation is more flexible and you can directly transfer the string literal value for reversal. A common solution is to define a new string with the passed String Length.

Equal character array, then copy the string, place STR characters from left to right into the character array, and then use loops to reverse the characters in the character array

*/

/* First, use the above solution, pass the const Type C style character pointer, and return the char * type */

// Assign values directly using character Arrays
Char * strrev1 (const char * Str)
{
Const size_t length = strlen (STR); // evaluate the character Length
Char * temp = new char [length]; // create an equal-length character array
Strcpy (temp, STR); // copy a string
For (size_t I = 0; I <= length/2; ++ I) // invert the characters in the character array. The cyclic execution condition is that the logo is less than or equal to half the character length.
{
Char c = temp [I];
Temp [I] = temp [length-I-1];
Temp [length-I-1] = C;
}
Return temp; // return the reversed character
}

// Use Pointer operations
Char * strrev2 (const char * Str)
{
Char * TMP = new char [strlen (STR)];
Strcpy (TMP, STR );

Char * ret = TMP; // returns the array pointer.
Char * P = TMP + strlen (STR)-1;
While (P> TMP)
{
Char T = * TMP;
* TMP ++ = * P;
* P -- = T;
}
Return ret;
}

// It is basically similar to the previous function, except that this function uses a shift operation to change the character pointer
Char * strrev3 (const char * Str)
{
Char * TMP = new char [strlen (STR) + 1];
Strcpy (TMP, STR );
Char * ret = TMP;
Char * P = TMP + strlen (STR)-1;
While (P> TMP)
{
* P ^ = * TMP;
* TMP ++ ^ = * P;
* P -- ^ = * TMP;
}
Return ret;
}

// Save a few steps and assign a reverse value to the new character array. It is simple and clear, but it is executed several times in a loop.
Char * strrev4 (const char * Str)
{
Char * temp = new char [strlen (STR)];
For (INT I = 0; I <= strlen (STR); I ++)
{
Temp [I] = STR [strlen (STR)-I-1];
}
Return temp;
}

// Use recursion to reverse the character and read it online. However, it does not feel good and there are too many restrictions.

// (You cannot directly reverse a constant string because a new character array is not defined. Instead, character inversion is directly performed on the original array to save space and improve efficiency. You also need to pass the character length and add restrictions, but it is always an idea)
Char * strrev5 (char * STR, int Len)
{
If (LEN <= 1)
Return STR;
Char T = * STR;
* STR = * (STR + len-1 );
* (STR + len-1) = T;
Return (strrev5 (STR + 1, len-2)-1 );
}

 

/* If it is a string of the STD: string type and is a const !!

The return type is STD: string.

*/

STD: String strrev6 (const STD: String Str)

{

String R; // defines a new string variable to receive

String R;
For (INT I = 0; I <Str. Length (); ++ I)
{
R = STR [I] + R; // attention sequence
}
Return R;

}

 

/* If it is a STD: String string, but not a const !!

The return type is STD: String, so you do not need to define a new string variable.

Space saving. Note: The string type can directly accept the string literal value ..

*/

STD: String strrev6 (STD: String Str)

{

For (INT I = 0; I <= Str. Length ()/2; ++ I)
{
Char c = STR [I];
STR [I] = STR [Str. Length ()-I-1];
STR [Str. Length ()-I-1] = C;
}
Return STR;

}

 

Related Article

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.