Skip to content

Instantly share code, notes, and snippets.

@peterc
Created May 27, 2010 15:55
Show Gist options
  • Save peterc/415975 to your computer and use it in GitHub Desktop.
Save peterc/415975 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
void condense_by_removing(char *z_terminated, char char_to_remove);
void condense_by_removing(char *z_terminated, char char_to_remove) {
int i, j = 0;
for (i = 0; i <= strlen(z_terminated); i++)
if (z_terminated[i] != char_to_remove)
z_terminated[j++] = z_terminated[i];
}
int main() {
char s[] = "hello world";
condense_by_removing(s, 'l');
printf("%s\n", s);
}
@entmike
Copy link

entmike commented May 27, 2010

include <stdio.h>

// Prototype
void condense_by_removing(char *z_terminated, char char_to_remove);
// BEGIN
main() {
char test_string[] = "Hello, World. This is a test.";

printf("\n\nOrig String: %s", test_string);
condense_by_removing(test_string, ' ' );
printf("\n\nNew String: %s\n\n", test_string);
return 0;
}
// Function
void condense_by_removing(char _z_terminated, char char_to_remove) {
char *new_string = z_terminated;
while(_z_terminated != '\0') {
if(*z_terminated != char_to_remove) *new_string++ = *z_terminated;
*z_terminated++;
}
*new_string = '\0';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment