Tuesday, September 4, 2012

Removing consecutive Zeros

 
/*Remove all two consecutive zeros in the given string.
 Example: a3409jd00dk000d
 Output: a3409jddk000d
 Note: If there are less/more than two consecutive zeros, they should not be replaced.
 */

void remove2zeros(char *input) { char *temp = input; while (*temp != '\0') { if (*temp == '0' && *(temp - 1) != '0' && *(temp + 1) == '0' && *(temp + 2) != '0') { temp = temp + 2; } else { if (input != temp) { *input++ = *temp++; } else { temp++; input++; } } } *input = '\0'; }

No comments:

Post a Comment