2011-03-06 wcdj
Problem description:
Write a string processing function in the following form. Copy the string pointed to by S1 to S2 reversely. For example, if S1 = "hello", make S2 = "olleh ". And no other variables except S1 and S2 can be used.
Void reversestr (const char * S1, char * S2 );
Problem Source
: Discussion
Several implementation methods are summarized as follows:
Method 1: Recursion
# Include <cstdio> <br/> void reversestr (const char * S1, char * S2) <br/>{< br/> If (* S1) <br/>{< br/> reversestr (S1 + 1, S2); // recursion <br/> while (* S2) <br/> S2 ++; <br/> * S2 = * S1; <br/> * (s2 + 1) = 0; <br/>}< br/> else * S2 = 0; <br/>}< br/> int main () <br/>{< br/> char DST [128] = {0 }; <br/> reversestr ("wcdj", DST); // DST = "jdcw" <br/> return 0; <br/>}< br/>
Method 2: Set the flag bit, that is, add a sentry
# Include <cstdio> <br/> void reversestr (const char * S1, char * S2) <br/> {<br/> // Add a sentinel to the S2 header <br/> * S2 = '/0'; <br/> // make S1, both S2 reach the last character position of the string <br/> while (* S1) <br/> {<br/> ++ S1, ++ S2; <br/> * S2 = '#'; // fill in the character <br/>}< br/> // Add a terminator to the end of S2 <br/> + + S2; <br/> * S2 = '/0'; <br/> -- S2; <br/> // position S2 to the header using the S2. <br/> while (* S2) <br/> -- S2; </P> <p> // start data replication <br/> -- S1; <br/> DO <br/> {<br/> * S2 = * S1; <br/> ++ S2; <br/> -- S1; <br/>}< br/> while (* S2 ); <br/>}< br/> int main () <br/>{< br/> char DST [128] = {0 }; <br/> reversestr ("wcdj", DST); // DST = "jdcw" <br/> return 0; <br/>}< br/>
Method 3: Use the memory instead of variables and store the length in S2.
# Include <cstdio> <br/> void reversestr (const char * S1, char * S2) <br/>{< br/> * (int *) S2) = 0; <br/> while (* S1) <br/>{< br/> S1 ++; <br/> * (int *) S2) + = 1; <br/>}< br/> S1-= * (int *) S2); <br/> S2 + = * (int *) s2); <br/> * S2 -- = '/0'; <br/> while (* S1) <br/>{< br/> * S2 -- = * S1 ++; <br/>}< br/> return; <br/>}< br/> int main () <br/>{< br/> char DST [128] = {0 }; <br/> reversestr ("wcdj", DST); // DST = "jdcw" <br/> return 0; <br/>}< br/>
Method 4: Use an exclusive or operation to exchange Variables
# Include <iostream> <br/> # include <cstring> <br/> using namespace STD; <br/> void reversestr (const char * S1, char * S2) <br/>{< br/> strcpy (S2, S1); <br/> while (s2 <S2 + 2 * strlen (S2)-strlen (S1)-1) <br/> {<br/> (* S2) = (* S2) ^ (* (s2 + 2 * strlen (S2)-strlen (S1)-1 )); <br/> (* (s2 + 2 * strlen (S2)-strlen (S1)-1) = (* S2) ^ (* (s2 + 2 * strlen (S2)-strlen (S1)-1); <br/> (* S2) = (* S2) ^ (* (s2 + 2 * strlen (S2)-strlen (S1)-1); <br/> ++ S2; <br/>}< br/> int main () <br/>{< br/> const char * S1 = "12345 "; <br/> char S2 [100]; <br/> reversestr (S1, S2); <br/> cout <S2 <Endl; <br/> return 0; <br/>}< br/>
Method 5: Use a String constant
# Include <iostream> <br/> using namespace STD; <br/> void reservestr (const char * C1, char * C2) <br/>{< br/> while (* C1! = '/0') <br/> + + C1; <br/> -- C1; <br/> while (* C1! = '/0') {<br/> * C2 = * C1; <br/> + + C2; <br/> -- C1; <br/>}< br/> * C2 = '/0'; <br/>}< br/> int main () <br/> {<br/> // char c1 [] = "hello"; // The loop will not stop. <br/> char * C1 = "hello "; // use a String constant <br/> char c2 [6]; <br/> reservestr (C1, C2); <br/> cout <C2 <Endl; <br/> return 0; <br/>}< br/>
Method 6: Use strlen and set '/0'
# Include <iostream> <br/> using namespace STD; <br/> void reversestr (char * S1, char * S2) <br/>{< br/> while (strlen (S1) <br/>{< br/> S2 [strlen (S2)] = S1 [strlen (S1) -1]; <br/> S1 [strlen (S1)-1] = '/0'; <br/>}< br/> cout <S2 <Endl; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> char S1 [] = "abcdefgh"; <br/> char S2 [1024]; <br/> memset (S2 ); <br/> reversestr (S1, S2); <br/> system ("pause"); <br/> return 0; <br/>}< br/>
Supplement: Implementation of strrev. C in CRT
/* <Br/> C:/Microsoft SDK/src/CRT/strrev. c <br/> * strrev. c-reverse a string in place <br/> * copyright (c) 1985-2001, Microsoft Corporation. all rights reserved. <br/> * purpose: <br/> * defines _ strrev () -Reverse a string in place (not including <br/> * '/0' character) <br/> * <br/> ******************************* **************************************** * *******/<br/>/***** <br/> * char * _ strrev (string) -Reverse a string in place <br/> * purpose: <br/> * reverses the order of Characters in the string. the terminating <br/> * null character remains in place. <br/> * entry: <br/> * char * string-string to reverse <br/> * Exit: <br/> * returns string-now with reversed characters <br/> * exceptions: <br/> * <br/> ******************************* **************************************** * *******/<br/> */<br/> # include <cstdio> <br/> char * _ cdecl _ strrev (<br/> char * string <br/>) <br/>{< br/> char * Start = string; <br/> char * Left = string; <br/> char ch; <br/> while (* string ++)/* Find end of string */<br/>; <br/> string-= 2; <br/> while (left <string) <br/> {<br/> CH = * left; <br/> * left ++ = * string; <br/> * string -- = CH; <br/>}< br/> return (start); <br/>}< br/> int main () <br/>{< br/> char SRC [] = "wcdj"; <br/> printf ("% s/n", _ strrev (SRC )); <br/> return 0; <br/>}< br/>