One pass multiple whitespace replace to single whitespace and eliminate leading and trailing whitesp
Posted
by Phoenix
on Stack Overflow
See other posts from Stack Overflow
or by Phoenix
Published on 2010-03-16T15:19:50Z
Indexed on
2010/03/16
15:21 UTC
Read the original article
Hit count: 538
whitespace
|removal
void RemoveSpace(char *String)
{ int i=0,y=0; int leading=0;
for(i=0,y=0;String[i]!='\0';i++,y++)
{
String[y]=String[i]; // let us copy the current character.
if(isspace(String[i])) // Is the current character a space?
{
if(isspace(String[i+1])||String[i+1]=='\0'||leading!=1) // leading space
y--;
}
else
leading=1;
}
String[y]='\0';
} Does this do the trick of removing leading and trailing whitespaces and replacing multiple whitespaces with single ones ?? i tested it for null string, all whitespaces, leading whitespaces and trailing whitespaces.
Do you think this is an efficient one pass solution ??
© Stack Overflow or respective owner