Created
May 28, 2010 19:13
-
-
Save drio/417588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* | |
Write a C routine with the following prototype: | |
void condense_by_removing( | |
char *z_terminated , | |
char char_to_remove | |
); | |
Your routine should modify the given zero-terminated string in place, | |
removing all instances of the given char. | |
*/ | |
void cbr(char *z , char cr) | |
{ | |
char *tmp; | |
int i,j; | |
tmp = (char *) malloc(sizeof(char) * sizeof(z)); | |
for(i=0,j=0; i<strlen(z); i++) { | |
if (z[i]!=cr) { | |
tmp[j]=z[i]; | |
j++; | |
} | |
} | |
strcpy(z, tmp); | |
free(tmp); | |
} | |
int main(int argc, char **argv) | |
{ | |
char *foo; | |
foo = (char *) malloc(2000 * sizeof(char)); | |
strcpy(foo, argv[1]); | |
foo[strlen(foo)]='\0'; | |
fprintf(stderr, "before: %s\n", foo); | |
cbr(foo, '_'); | |
fprintf(stderr, "after : %s\n", foo); | |
free(foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment