Skip to content

Instantly share code, notes, and snippets.

@KushalP
Created February 24, 2010 01:01
Show Gist options
  • Save KushalP/312937 to your computer and use it in GitHub Desktop.
Save KushalP/312937 to your computer and use it in GitHub Desktop.
/**
* Solution by KushalP
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void condense_by_removing(char *z_terminated, char char_to_remove);
int main(int argc, char *argv[])
{
char *s;
//This is a cli tool, so we must have enough input!
if (argc != 3 || strlen(argv[1]) != 1)
{
printf("Usage: %s <char> <string>\n", argv[0]);
return 1;
}
//Do we even have enough memory to be removing all
//vowels from the Back to the Future movie script?
if ((s = malloc(strlen(argv[2]) + 1)) == NULL)
{
printf("Cannot allocate memory for string\n");
return 1;
}
//Copy over our string
strcpy(s, argv[2]);
//Run our function
condense_by_removing(s, *argv[1]);
//Output our string to the cli
puts(s);
return 0;
}
void condense_by_removing(char *z_terminated, char char_to_remove)
{
char *new;
for (new = z_terminated; *z_terminated != '\0'; z_terminated++)
if (*z_terminated != char_to_remove) {
//As we're incrementing our input string, check if
//it's added to our new string
if (new != z_terminated)
*new = *z_terminated;
//Increment our new string along
new++;
}
*new = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment