One pass multiple whitespace replace to single whitespace and eliminate leading and trailing whitesp
- by Phoenix
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 ??