Skip to content

Instantly share code, notes, and snippets.

@abdalla-alothman
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save abdalla-alothman/411fa6e81a7ced4f48b3 to your computer and use it in GitHub Desktop.

Select an option

Save abdalla-alothman/411fa6e81a7ced4f48b3 to your computer and use it in GitHub Desktop.
Remove a character from a C string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//////////////////// Remove Character from String /////////////////////////////
char *removeChar(const char *check, const char remove)
{
char *p = malloc(sizeof(char) + 1);
unsigned short int f = 0;
for(unsigned short int x = 0; check[x] != '\0'; ++x)
{
if(check[x] != remove)
{
p = realloc(p, x * sizeof(char) + 1);
p[f++] = check[x];
}
}
p[f] = '\0';
return p;
}
///////////////////////////////////////////////////////////////////////////////
int main(void)
{
const char check[] = "T'his i's a tes't";
char *c2 = strchr(check, '\'');
if(c2)
{
c2 = removeChar(check, '\''); // remove apostrophe
printf("%s\n", c2);
free(c2); c2 = NULL;
}
puts(check);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment