Strip spaces

Posted by Zeno on Mon 25 Jul 2005 04:29 PM — 8 posts, 30,192 views.

USA #0
Just a random question. Is there a function that strips the spaces from a string? I'm asking in Smaug just in case none exists in *nix and Smaug has its own unique function to do it.
Canada #1
These is no system command that I know of, but I have one of my own:

char     *smash_space(const char *str)
{
        static char ret[MAX_STRING_LENGTH];
        char     *retptr;

        retptr = ret;
        for (; *str != '\0'; str++)
        {
                if (*str == ' ')
                        continue;
                else
                {
                        *retptr = *str;
                        retptr++;
                }
        }
        *retptr = '\0';
        return ret;
}
USA #2
Hm, I'll try it out thanks.

A quick side question. When I say "linux function" am I incorrect? Should I be saying "C function"? I'm not sure I totally understand then. Would that mean the function in Windows in C to move a window is the same function as one on linux?
USA #3
Quote:

A quick side question. When I say "linux function" am I incorrect? Should I be saying "C function"? I'm not sure I totally understand then. Would that mean the function in Windows in C to move a window is the same function as one on linux?


I guess technically you should be saying "ANSI C". While the systems have evolved and differ a bit, generally anything in the ANSI C standard will be found cross platform (such as strcmp, etc).

The Win32 API would definitely not be the same as a graphical API on linux. While the function names might be similar, they arent part of the standard, and are written specifically for their platform.

Of course you may find API's like SDL that are cross platform and control windows and various things, in which case you would be correct, as all of the function calls would be the same for any platform.
USA #4
Hmm if I'm using arg1 which is [MAX_STRING_LENGTH], how would I replace the string?
arg1 = smash_space(arg1)

Obviously doesn't work because they aren't the same type.
Canada #5
No, it returns a character pointer, so you need to store it into a character pointer variable.

char *arg;

sprintf(arg, "Before: %s\nAfter: %s", string, smash_space(string));
USA #6
But won't that cause incompatibles elsewhere? I have this already:
    sprintf( cfilename, "%s.cln", strlower( arg ));

Could I now just:
sprintf( cfilename, "%s", strip_space(cfilename);

Or would I need to change it to a pointer still?
Canada #7
Well, if you wanted to lower everything, you could use this:

    sprintf( cfilename, "%s.cln", strip_space(strlower( arg )));


Since strlower returns a character pointer, and the argument for strip_space is a character pointer, no incompatabilities.