4 ms·
You don't need all that logic in there. for (i = 0; i <= strlen(z_terminated); i++) if (z_terminated[i] != char_to_remove) z_terminated[write_index
by petercooper 16y ago
You don't need all that logic in there.
for (i = 0; i <= strlen(z_terminated); i++)
if (z_terminated[i] != char_to_remove)
z_terminated[write_index++] = z_terminated[i];
That even removes the need to add the \0 later on.
- dalke 16y agoFor extra performance, use strchr to find the first char_to_remove. /* Find the first character to remove */ /* This doubles the performance when there are no remove characters, I assume because it uses a builtin and because I don't do unneeded writes. */ src = strchr(z_terminated, char_to_remove); if (src == NULL) return; /* src is the position in the old string, with the possible characters to remove. dest is the end position of the new string, where non-removed characters are added */ dest = src++; for (;;) { /* Skip additional remove characters */ while (*src == char_to_remove) src++; /* Copy (including copying the terminal NUL) */ if (! (*dest++ = *src++)) { /* copied a NUL - end of string */ break; } }