Tyler Treat Tyler Treat - 1 month ago 10
C Question

Removing Spaces from a String in C?

What is the easiest and most efficient way to remove spaces from a string in C?

Answer

Easiest and most efficient don't usually go together...

Here's a possible solution (untested):

void RemoveSpaces(char* source)
{
  char* i = source;
  char* j = source;
  while(*j != 0)
  {
    *i = *j++;
    if(*i != ' ')
      i++;
  }
  *i = 0;
}