2. String processing
This type of probability is large, so it must be noted that this type of question is not difficult but careful. Remove the following exceptions and content inverse.
Example 6. Assuming that the input string contains letters and * numbers. Please prepare a functionality FUN (), which is: move all * numbers before the string to the string to the string
For example: Original string: ****** a * bc * def * g ****
String after processing: a * bc * def * g **********
Note: Some source programs have been given.
Do not change the contents of the main function main ().
Question procedure:
#include
#include
Void Fun (char * a)
{
}
Main ()
{
Char s [81], * p;
INT n = 0;
CLRSCR ();
Printf ("/ N / NINPUT A STRING S:");
Gets (s);
Fun (s);
Printf ("/ N / NTHE STRING AFTER MOVING:"); PUTS (S);
}
Difficulties in this question: How is internal to move?
at first:
**** a * bc ** DEF * g ****
First move:
*** a * bc ** DEF * g ****
*
First move:
* ** a * bc ** DEF * g ****
*
First move:
** * a * bc ** DEF * g ****
*
First move:
*** a * bc ** DEF * g ****
*
. . . . . .
First move:
*** a * bc ** DEF * g *****
*
Can repeat four times, it will
The following method can also be used
at first:
**** a * bc ** DEF * g ****
the first time:
A *** * bc ** DEF * g ****
* 4
the second time:
A *** bc ** DEF * g ****
* 4
Move N-4 times, then add 4 * after adding.
Reference answer:
Void Fun (char * a)
{
INT i = 0, n = 0, j = 0;
While (a [i] == '*')
{
i ;
N ;
} / * Calculate the length of all * numbers before the first letter * /
While (a [j]! = '/ 0')
{
J ;
} / * Calculate the length of the string * /
For (i = 0; i { Printf ("% C", A [N I]); a [i] = a [n i]; } For (i = j-n; i <= j; i ) { a [i] = '*'; } A [J] = '/ 0'; }