2019-04-15, 10:39 PM
Another way you can do this is to loop through once, and shift all letters backwards. Every time you see a space you increase this shift offset:
Is this better? I don't know. It will depend on the length of the string and the number of double spaces.
PHP Code:
DeSpace(string[])
{
new
// Read index.
i = -1,
// Write index.
j = -1,
// Current character.
ch,
// Did we already see a space?
bool:space;
do
{
if ((ch = string[]) != ' ')
{
// Not a space, just copy it.
string[] = ch;
// Mark us as having NOT just seen a space.
space = false;
}
else if (!space)
{
// First space. Any more just ignore them.
string[] = ch;
// Mark us as having just seen a space.
space = true;
}
}
while (ch);
}
Is this better? I don't know. It will depend on the length of the string and the number of double spaces.