On Mon, 27 Nov 1995, Chaotic wrote: > On Sun, 26 Nov 1995 myers@indy.net wrote: > > > > 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. :) ^^^^^^^^^^^^^^^^^^^^^^ > > > > Really ;) > > > > Sorry, could not resist. ;) > > i don't blame you, i would have responded also. makes me glad i added > the caveat though. :) Although, you shouldn't have put that guarantee in there... > however, i don't think you can make it much faster. those functions > using calls to strtok() would have been sloooOOOooow. > > Lauren > Wouldn't it depend on how many \r's there were in the line though? I mean, if there's only one then the speed difference should be marginal, right? But that's beside the fact that your function does 2 things wrong. First, it overwrites the original string so you can't use it to output to the mud anymore. And second, well, it doesn't work. :P I had a feeling so I tested it out with the following: [----remove_CRs.c----] #include <stdio.h> void remove_CRs(char *string); main() { char string[] = "this\r i\rs a \rtes\rt\r"; FILE *fp; fp = fopen("remove_CRs.test", "w"); fprintf(fp, "%s", string); remove_CRs(string); fprintf(fp, "\n\n%s", string); fclose(fp); } void remove_CRs(char *string) { register char *ptr; ptr = string; do { if (*string != '\r') *ptr++ = *string; ++string; } while(*string); } [----End of file----] Notice: I didn't touch your function except to fix the == thing. This is the output file: [----remove_CRs.test----] this i s a tes t this is a testes t [----End of file----] So unless I did something wrong, this thing needs to go back to the drawing board. So much for the "guarantee"... I WANT MY MONEY BACK! What? Oh, nevermind. I forgot it was free. You don't happen to work for Macro$oft do you? (No, I'm sorry. I'm just a little resentful cause you put down my nice little program like it was a piece of garbage! :( Godfunit... Hey, I only spent 5 mins on the damn thing--and I wasn't claiming it was the best, either.) Windoze'95: I can't get no satisfaction, cause I try, and I try, and I try...
This archive was generated by hypermail 2b30 : 12/07/00 PST