Requirement: do not apply for variables or space reversal strings. Use a function. A typical application of exclusive or ^ exchange or addition/subtraction exchange! Via exam
Basic Idea: Exchange characters between two ends. The last character of the string is '/0', indicating the end. It has no practical significance. You can use it as an intermediate variable. After processing, set the last character to'/0 '.
**************************************** *******************************
Void reverse (char * s)
{
If (strlen (S) = 1) // when it is a character, there is no need to swap or odd number
Return;
If (* (s) // 0 or even
{
* S = * s + * (S + strlen (S)-1 );
* (S + strlen (S)-1) = * s-* (S + strlen (S)-1 );
* S = * s-* (S + strlen (S)-1 );
// Swap the first and last characters of a string, but addition may overflow.
* (Unsigned short *) (S + strlen (S)-1) = (* (S + strlen (S)-1) <8;
// Move the last byte of the string back to one byte using 0 = '/0'
// Of course, you can also use the character exchange method above, but the efficiency of shift using 0 = '/0' is higher.
// The essence of the above four steps is to implement two-to-two SWAps, the simplest of which is two-to-two SWAps.
// But the ending character is known. Put the first character directly at the end, and the last character put the first character.
// Enter the last ending character in three steps, which is easy to understand, provided that a variable is used to save the length of the string. //
// However, because temporary variables cannot be applied, the strlen function cannot be used after the ending symbol is moved.
// Therefore, the forced conversion method is unique.
// Then, the new string with the first and last characters removed is passed in and called recursively.
Reverse (S + 1 );
* (Unsigned short *) (S + strlen (s) = * (S + strlen (s) + 1 );
// Move '/0' step by step to restore the changed substring
}
Else
Return;
}
Graphical process: String abcdef
S + 1 = STR = abcdef '/0 'abcdef'/0'> fbcdea '/0'> fbcde'/0'
S + 1 = STR = bcde '/0 fbcde'/0 'a> fecdb'/0 'a> fecd'/0' Ba
S + 1 = STR = Cd'/0 'fecd'/0' BA> fedc'/0' BA> fed '/0' CBA
S + 1 = STR = '/0', return recursively, at this time, S = CD '/0' has been changed in the previous step to d'/0' C> DC '/0'> edcb'/0'> fedcba '/0'
When the length of a string is an odd number, if it is an odd number, the length of the string that is finally passed in is 1. In this case, no interaction is required and you should exit directly. Therefore, the entry is supplemented to determine the parity condition.
If (strlen (S) = 1)
Return;
Of course, you can also determine the modified String Length if (strlen (STR + 1)> = 2) Before recursive calls, which can eliminate the influence of odd numbers.
When the original string is one, there should be no processing for the first time, so it is more reasonable to judge at the entrance.
×××××××××××××××××××××××××××××××××
Optimized. No variables are applied. chained operations are supported. char * is returned *
Char * reverse (char * s)
{
If (strlen (S) = 1)
Return S;
If (* (s ))
{
* S = * s + * (S + strlen (S)-1 );
* (S + strlen (S)-1) = * s-* (S + strlen (S)-1 );
* S = * s-* (S + strlen (S)-1 );
// Swap the first and last characters of a string, but addition may overflow.
* (Unsigned short *) (S + strlen (S)-1) = (* (S + strlen (S)-1) <8;
Reverse (S + 1 );
* (Unsigned short *) (S + strlen (s) = * (S + strlen (s) + 1 );
}
Return s; // if this sentence does not exist, the program will be wrong, because the outermost layer of recursion enters if (* (s). At this time, no return value is returned.
}
×××××××××××××××××××××××××××××××××
I have applied for a variable to save the length of the string. Generally, I can interview this program. I can write this article, but it's actually very cool, it is estimated that others have started to suspect that they have done this question before.
Char * reverse (char * s)
{
If (strlen (S) = 1)
Return S;
If (* (s ))
{
Int Len = strlen (s );
* (S + Len) = * s;
* S = * (S + len-1 );
* (S + len-1) = '/0 ';
// Start/end character and end character interchange position
// Remove the first and last characters before recursive calling
Reverse (S + 1 );
// * (Unsigned short *) (S + strlen (s) = * (S + strlen (s) + 1 );
* (S + len-1) = * (S + Len );
* (S + Len) = '/0'; // restore the originally changed string
}
Return S;
}
×××××××××××××××××××××××××××××××××
Recursively move the last element of the string to the head of the team, recursively arrive at the end of the team, exchange with an exclusive or ^, move the element at the end of the team to a grid, return to the upper layer, switch again, and finally move the end of the team to the head of the team
Void tail2head (char * s)
{
If (strlen (S) = 1)
{
Return;
}
Else if (strlen (S) = 2)
{
S [0] = s [1] ^ s [0];
S [1] = s [1] ^ s [0];
S [0] = s [1] ^ s [0];
Return;
}
Else if (strlen (s)> 2)
{
Tail2head (S + 1 );
S [0] = s [1] ^ s [0];
S [1] = s [1] ^ s [0];
S [0] = s [1] ^ s [0];
}
}
Recursively move the tail of a team to the team's head, update the team's head to the next element, call tail2head again, and then all the reverse orders are completed.
Char * reverse (char * s)
{
If (strlen (s)> 1)
{
Tail2head (s );
Reverse (S + 1 );
}
Return S;
}
This method has the largest number of elements to move, but the idea is clear. In addition, the whole function is implemented using two recursive functions, breaking the fixed thinking that there is usually only one interface function.
Main ()
{
Char streven [10] = "abcdef ";
Char strood [10] = "1234567 ";
Char strone [10] = "1 ";
Char strtwo [10] = "12 ";
Printf ("string 'abcdef 'reversed is' % s'/N", reverse (streven ));
Printf ("string '000000' reversed is '% s'/N", reverse (strood ));
Printf ("string '1' reversed is '% s'/N", reverse (strone ));
Printf ("string '12' reversed is '% s'/N", reverse (strtwo ));
Return 0;
// String 'abcdef' reversed is 'fedba'
// String '000000' reversed is '000000' 1234567? 'When the length of a string is odd, it seems that there is still a problem.
// String '000000' reversed is '000000'. The condition is correct after being supplemented.
// String '1' reversed is '1'
// String '12' reversed is '21'
}