Re: ^Ms, easier solution correction!

From: Chaotic (chaotic@eskimo.com)
Date: 11/26/95


On Sat, 25 Nov 1995, The Awakening wrote:

> It never fails that when you edit a function on the fly before mailing 
> it, you'll screw up.  Originally the cleanup function returned void and 
> just modified *dest.  Well, in changing it to return a char * so you 
> could use it inside fprint and such, I forgot to make a couple of 
> necessary changes.  So, here's the correct version (hehehe, maybe I 
> should practice playing with strings).
> 
> /* strips \r's from line */
> char *cleanup(char *dest, const char *src)
> {
>   if (!dest || !src)
>     return NULL;
> 
>   char *temp = &dest[0];
>   int i = 0, length = strlen(src);
> 
>   for (; *src && (i < length); i++, src++)
>      if (*src != '\r')
>        *(temp++) = *src;
> 
>   // terminate it properly
>   *temp = '\0';
> 
>   return dest;
> }
> 
> 
> This should work correctly now (=.
> 

i've been a lurker in this group for about a year an a-half and never
posted (i read this group mostly for ideas), but being a software engineer
for 15 years specializing in low level C and assembly embedded systems,
sloppy code will sometimes iritate me.  :)

you don't need to return anything... do the string in place:

[........................ snip .........................]

void remove_CRs(char *string)
{
    register char *ptr;

    ptr = string;
    do {
        if (*string != '\r') 
            *ptr++ == *string;
        ++string;
    } while(*string);
}

[......................... snip .........................]

this does it all, including placing the endmark on the string.  it's also 
compact and FAST.  caveat: untested, but guaranteed to work. :)

Lauren

ps. if you want to make it more generic, pass the character.  ie:

    void remove_a_char(char *string, char char_to_remove), and replace 
    the '\r' with char_to_remove.



This archive was generated by hypermail 2b30 : 12/07/00 PST