4 ms·
Yeah, I had almost exactly the same code. I'm sure most people did too. The only other "obvious" algorithm is the n^2 one.
by aplusbi 16y ago
Yeah, I had almost exactly the same code. I'm sure most people did too. The only other "obvious" algorithm is the n^2 one.
- RiderOfGiraffes 16y agoYou will be surprised.
- aplusbi 16y agoI like surprises! Looking forward to part 3...
- Xurinos 16y agoI use do...while instead of the for loop, making the usual nasty C-programmer use of ++ in the right spots, but other than that, my solution is the same. Edit: Correction... I also copy the '\0' character before exiting instead of making that a separate step. It has to do with the order of things. Here is the code: char* read_ptr = s; do { if (*read_ptr != c) { *s++ = *read_ptr; } } while (*read_ptr++ != '\0');
- philwelch 16y agoYou can use an inner loop to skip over removable chars in the source string but I didn't think that was n^2. (Code below for reference.) Am I wrong that my solution is n, or is there another obvious n^2 algorithm? void condense_by_removing( char *z_terminated , char char_to_remove) { char * sp; char * dp; sp = dp = z_terminated; while (*sp != '\0') { while (*sp == char_to_remove){ sp++; } *dp = *sp; dp++; sp++; } *dp = *sp; }