Last active
August 29, 2015 14:06
-
-
Save abdalla-alothman/411fa6e81a7ced4f48b3 to your computer and use it in GitHub Desktop.
Remove a character from a C string
This file contains hidden or 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> | |
| //////////////////// 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