Count on it, I am always very hesitant to post code on HN, realizing that if I'm not fresh or have tested the code that I'll be mercilessly hacked to little bits. It's fun though, I think my personal record is 5 bugs in 2 lines...
Count on it, I am always very hesitant to post code on HN, realizing that if I'm not fresh or have tested the code that I'll be mercilessly hacked to little bits. It's fun though, I think my personal record is 5 bugs in 2 lines...
Your mettle has never truly been tested until you have hundreds of developers, from around the world, pointing out your idiocy in an excruciatingly specific manner. After your ego has been throughly decimated, then you can improve.
This is closer to how I would do it in practice.
void condense_by_removing (char *z_terminated, char char_to_remove)
{
char *next = z_terminated;
while (1) {
while (*z_terminated == char_to_remove) ++z_terminated;
if (!(*next++ = *z_terminated++) ) break;
}
}
EDIT: Fixed bugI have a similar solution farther down the page. Not many people seem to like doing it with pointer manipulation.
EDIT: We should both probably add
if(!char_to_remove) return;
at the top since we'll get a seg fault if someone tries to be sneaky and passes the zero-terminator. And then might as well check if z_terminated is NULL while we're at it. Or is there a better way to handle it?Aye... and that's when you learn. I've worked for a bit for a guy that had learned C in the 70's, best school I've had... also very painful at times.
I think you owe 'bwithe' a beer.