Remove spaces from self-written strings. c Function
1) extract the specified character C from string S.
Static char * trim (char * s, char C)
{
Return (* s = 0 )? S :( (* s! = C )? (TRIM (S + 1, C)-1) = s )? S :( * (TRIM (S + 1, C)-1) = * s, * s = C, trim (S + 1, C ))): trim (S + 1, C )));
}
Example:
Char STR [] = "m y B eautiful C hin! ";
Char * s = trim (STR, 32 );
+ S 0x0030fd0f "mybeautifulchina! "Char *
2) Remove the specified character C from string S.
Static char * ltrim (char * s, char C)
{
While (* s! = 0 & * s = c) {s ++;} return S;
}
Example:
Char STR [] = "m y B eautiful C hin! ";
Char * s = ltrim (STR, 32 );
+ S 0x0028f804 "m y B eautiful C hin! "Char *
2) Remove the specified character C from string S.
Static char * rtrim (char * s, char C)
{
Char * P = s, * q = s; while (* P! = 0) {If (* P! = C) {q = P; q ++;} p ++;} If (Q! = S) * q = 0; return S;
}
Example:
Char STR [] = "m y B eautiful C hin! ";
Char * s = rtrim (STR, 32 );
+ S 0x002cf6fc "m y B eautiful C hin! "Char *
Char * s = rtrim (ltrim (STR, 32), 32 );
+ S 0x0031fa78 "m y B eautiful C hin! "Char *