Compile a string collation function ver (char *) to remove leading and trailing white spaces in known strings, and the number of consecutive white-space characters between non-white-space characters in the string is reduced to one
For this question, you can also set the status for processing. At the beginning, set flg to 0, indicating that the white space class character that the program will encounter is the leading blank character of the string; if flg is 1, a blank class character is encountered during the copy process;
2 indicates that a blank character has been processed.
1 char * ver (char * s)
2 {
3 int flg = 0;
4 char * p = s, * q = s;
5 for (; * s; s ++) {/* access each character in string s in sequence */
6 if (* s = ''| * s = 'T' | * s = 'n')/* blank class characters */
7 flg = (flg = 2 )? 1: flg;
8 else {
9 if (flg = 1)
10 * q ++ = '';/* When a blank class character is encountered, copy the next blank character */
11 * q ++ = * s;/* copy the current character */
12 flag = 2;/* processing of an intermediate blank character column ends */
13}
14}
15 * q = '';
16 return p;
17}
Another method for searching online
String ls_string, ls_string_before, ls_string_after
Integer li_tab, li_blank, li_blk_dbcs
Ls_string = trim (sle_1.text)
// When processing TAB characters, PB can only be used for POS without the need for POSW.
Li_tab = Pos (ls_string ,"~ T ")
Do while li_tab> 0
Ls_string_before = Mid (ls_string, 1, li_tab-1)
Ls_string_after = Mid (ls_string, li_tab + 1)
Ls_string = ls_string_before + ls_string_after
SetNull (li_tab)
Li_tab = Pos (ls_string ,"~ T ")
LOOP
// Delete the halfwidth Space
Li_blank = pos (ls_string ,'')
Do while li_blank> 0
Ls_string_before = Mid (ls_string, 1, li_blank-1)
Ls_string_after = Mid (ls_string, li_blank + 1)
Ls_string = ls_string_before + ls_string_after
SetNull (li_blank)
Li_blank = pos (ls_string ,'')
LOOP
// Delete Spaces
Li_blank_dbcs = PosW (ls_string ,'')
Do while li_blank_dbcs> 0
Ls_string_before = MidW (ls_string, 1, li_blk_dbcs-1)
Ls_string_after = MidW (ls_string, li_blk_dbcs + 1)
Ls_string = ls_string_before + ls_string_after
SetNull (li_blk_dbcs)
Li_blank_dbcs = PosW (ls_string ,'')
LOOP
Method 4
# Include <stdio. h>
# Include <string. h>
# Include <iostream. h>
# Include <math. h>
Void fun (char * str );
Int main ()
{
Char str [8] = "a bcd ";
Fun (str );
Cout <str <endl;
Return 0;
}
Void fun (char * str)
{
Int I, n = strlen (str );
Cout <n <endl;
Char * p = str;
For (I = 0; I <n ;)
{
If (* p! = '')
Str [I ++] = * p;
P ++;
}
Str [I] = '';
}
Instance
Char * ver (char * s)
{Char * p = s, * q = s;
/* Pass the leading blank space character */
For (; * s = ''| * s = 'T' | * s = 'n'; s ++ );
For (; * s;) {/* sequential access to each character in string s */
* Q ++ = (* s = 'T' | * s = 'n ')? '': * S;
If (* s! = ''& * S! = 'T' & * s! = 'N') s ++;
Else
While (* s! = ''| * S = 'T' | * s = 'n') s ++;
}
If (q> p & * (q-1) = '')/* For example, q> p, non-blank characters have been copied */
* (Q-1) = '';/* If the last copied is a blank character, change it to the string Terminator */
Else
* Q = '';/* Otherwise, add the string Terminator */
Return p;
}