C/c ++: How to delete spaces in a string

Source: Internet
Author: User


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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.