Character string Movement (if the character string is a combination of * and 26 letters, move * to the leftmost, move the letters to the rightmost, and keep the relative order unchanged ), minimum Time and space complexity
If there is no requirement to ensure that the relative sequence of letters remains unchanged, it is the easiest to move the exchange element to the center using two double-ended pointers. However, this movement has a jump, so the relative order cannot be guaranteed.
For the method, see the notes:
The main idea is to use a point pointer to point to the first * at the end, and then use the pointer let to point to the first letter before, in turn, the letter and * sequence at the end of let and point are exchanged. Then find the next letter and * sequence until a pointer points to the first address of the string.
To scan from the back to the back, you need to swap the numbers and letters you encounter, so that you can ensure that the subsequent letters are still behind.
# Include "stdafx. H "# include <iostream> using namespace STD; void switcher (char * Str) {int Len = strlen (STR); char * Let = STR + Len; char * point = STR + Len; while (point! = STR & let! = Str) // cyclic termination condition {While (* point! = '*' & Point! = Str) // find the first * {point --;} If (point = Str) from the back and forth // if the first address of the string is found, that indicates that the entire string is * return; let = point; while (* Let = '*' & let! = Str) // start from the first * and find the first letter {Let --;} while (* let! = '*' & * Point = '*' & let! = Str) // exchange letters and * {// cout <* Let <"<-->" <* point <Endl; char CH = * Let; * Let = * point; * point = CH; let --; point -- ;}} int main () {char a [] = "*** AB ** CD *** e ** DF ** Ghi"; cout <A <Endl; int Len = sizeof () /sizeof (A [0]); switcher (a); cout <Endl; getchar (); Return 0 ;}