Copy Code code as follows:
Through different methods, to achieve the inversion of the input string, can be a good review to consolidate the basic knowledge of C + +
/* Analysis Process:
Suppose you want to make the string passed as a constant const string, which is more flexible, and can be directly passed the string literal to reverse, and the common solution is to define a new one and pass it over the length of the string
An equal array of characters, then a string copy, placing the str characters from left to right into the character array, and then looping to reverse the characters in the character array
*/
/* First, using the above thought solution, passed to the const type C-style character pointer, returned to the char* type * *
Assigning values directly using a character array
char* strrev1 (const char* STR)
{
Const size_t length = strlen (str);//Find characters
Char *temp = new char[length];//creates an array of equal-length characters
strcpy (TEMP,STR);//String copy
for (size_t i = 0; I <= length/2 ++i)//Reverses the character in the character array, and the loop execution condition is that the identity is less than or equal to half the length of the character
{
char C = temp[i];
Temp[i] = temp[length-i-1];
Temp[length-i-1] = C;
}
Return temp;//returns the reversed character
}
Using pointer operation mode
char* strrev2 (const char* STR)
{
char* tmp = new Char[strlen (str)];
strcpy (TMP,STR);
char* ret = tmp;//used to finally return array pointers
char* p = tmp + strlen (str)-1;
while (P > tmp)
{
char t = *tmp;
*tmp++ = *p;
*p--= t;
}
return ret;
}
is basically similar to the previous function, except that this function uses a shift operation to change the character pointer pointing
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, directly to the new character array to the inverse of the value, oh, simple and clear, but the cycle of more than a few executions
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;
}
Using recursion to invert characters, see on the internet, but feel bad, limit too much
(You cannot invert a constant string directly, because you don't define a new character array, but you do a character inversion directly on the original array, saving space, improving efficiency, passing the length of the character, increasing the limit, but it's always a thought)
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 the pass is a std::string type string and is const!!
return type is std::string
*/
std::string strrev6 (const std::string str)
{
String R//defines a new string variable that is used to receive
String R;
for (int i = 0; i < str.length (); ++i)
{
r = str[i] + r;//Note order
}
return R;
}
/* If the pass is std::string type string, but not const!!
Return type is std::string, then you will no longer have to define a new string variable
Space-saving Note: String literals can be directly accepted as literal values.
*/
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;
}