Re:Interviewing programmers: coding test example explained
by dkersten on Thursday June 03, 2010 @04:52PM
(#1402507)
Replacing the 3 additions of gapin the loop with a single subtraction (and an increment):
void condense_by_removing(char* z_terminated, char char_to_remove) {
int offset = 0;
for (; *z_terminated; ++z_terminated) {
if (*z_terminated == char_to_remove) {
++offset;
++z_terminated;
}
if (offset > 0) *(z_terminated - offset) = *z_terminated;
}
*(z_terminated - offset) = 0;
}
Re:Interviewing programmers: coding test example explained
by RiderOfGiraffes on Thursday June 03, 2010 @03:13PM
(#1402206)
Have you sent it in? You should ...
The second version is identical to the first, except that it ises the z_terminated pointer instead of a separate index.
Interestingly enough, if I try to remove the common *(z_terminated + gap) expression (which a good compiler should do for me..), I end up with a two pointer version similar to yours.