Skip to content

Instantly share code, notes, and snippets.

@greyfade
Created May 27, 2010 18:03
Show Gist options
  • Save greyfade/416128 to your computer and use it in GitHub Desktop.
Save greyfade/416128 to your computer and use it in GitHub Desktop.
/* I'm a C++ programmer by day, so when I saw this problem, I thought it would
* be worthwhile to hack it up. I changed the variable names to help me think
* about the problem, not for some kind of obfuscation. I'm curious how my
* solution compares to solutions devised by C programmers. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void condense_by_removing(char *s, char rm) {
/* Assume `s` is a null-terminated string. */
char *s1 = s;
char *s2 = s;
/* No sense in continuing if \0 is to be removed or s is null. */
if(rm == 0 || s == 0) return;
while(*s2) {
if(*s2 == rm) ++s2;
else *s1++ = *s2++;
}
*s1 = '\0';
}
const size_t MAX_BUF = 1024*1024*sizeof(char);
int main(int argc, char **argv) {
char* str;
if(argc < 2) return EXIT_FAILURE;
str = malloc(MAX_BUF);
if(!str) return EXIT_FAILURE;
while(fgets(str, MAX_BUF, stdin)) {
condense_by_removing(str, argv[1][0]);
printf("%s", str);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment