Summary of string inversion operations

Source: Internet
Author: User
Tags strlen
//without any C-language functions: Strlen,cout, strcpy, char * strrev (char * str)
{
char *start = str;
char *left = str;
Char ch;

while (*str++); /* Find end of String */

STR-= 2;

while (left < STR)
{
ch = *left;
*left++ = *str;
*str--= ch;
}

return (start);
}




Solution One: The first time to see this topic, think of the simplest and most intuitive solution is: the traversal of the string, the first character and the last exchange, the second and the penultimate exchange, sequentially loop, then the first solution:

char* strrev1 (const char* STR)

{

int len = strlen (str);

char* tmp = new Char[len + 1];

strcpy (TMP,STR);

for (int i = 0; i < LEN/2; ++i)

{

char C = tmp[i];

Tmp[i] = tmp[len–i-1];

TMP[LEN–I-1] = c;

}

return TMP;

}

Here is the character that accesses the string through the subscript of the array, which is actually done directly with the pointer. The second solution is based on this, the implementation code is:

char* strrev2 (const char* STR)

{

char* tmp = new Char[strlen (str) + 1];

strcpy (TMP,STR);

char* ret = tmp;

Char *ret =str;

char* p = tmp + strlen (str)-1;

while (P > tmp)

{

char t = *tmp;

*tmp = *p;

*p = t;

--p;

++tmp;

}

return ret;

}

Obviously the above two solution does not take into account the optimization of time and space, a typical optimization strategy is two character Exchange algorithm optimization, we can completely do not use any external variables to complete the two characters (or integer) Exchange, this is a very classic interview topic. In particular, some embedded hardware-related programming often takes into account the use of registers, so there are often no third register to complete the two register data exchange problem. Generally there are two solutions, corresponding to the solution of the three-reconciliation method four.

The implementation code for solution three is:

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;

--p;

++tmp;

}

return ret;

}

The implementation code for solution four is:

char* strrev4 (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 = *p + *tmp;

*tmp = *p-*tmp;

*p = *p-*tmp;

--p;

++tmp;

}

return ret;

}

In fact, we can also solve this problem through the idea of recursion, the idea is simple: each exchange of two characters, the middle part of the same problem as the original string, so you can solve the problem by the idea of recursion, corresponding to the solution five implementation code:

char* strrev5 (/*const */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);

}

A test program is given below:

int main (int argc,char* argv[])

{

       char* str = "Hello";

       p (str);

 

       char* str2 = Strrev1 (str);

       p (str2);

 

       char* str3 = Strrev2 (str2);

       p (STR3);

 

       char* STR4 = Strrev3 (STR3);

       p (STR4);

 

       char* STR5 = strrev4 (STR4);

       p (STR5);

 

       char* STR6 = strrev5 (Str5,strlen (STR5));

       p (STR6);

      

       return 0;

}

You can see the string "Hello" and "Olleh" alternating output.

Description: 1) This method does not seriously consider the legitimacy of the input string and the special length (such as null, a character, etc.) string processing; 2) The first 4 algorithms do not change the input string value, the solution five modified the input string.

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.