Last active
February 26, 2019 00:51
-
-
Save DrFrankenstein/0566551d40b6654987e45f8ae57328aa to your computer and use it in GitHub Desktop.
C String splitting examples
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 <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
void split_in_two(const char* input) | |
{ | |
// we first make a copy of the input. We don't have to, | |
// but strsep operates by overwriting the delimiter in the | |
// input string, and I want to keep it clean. | |
// you can remove that step if you're okay with trashing | |
// the original. | |
char* source = malloc(strlen(input) + 1); | |
if (!source) | |
abort(); | |
strcpy(source, input); | |
char left[50], right[50]; | |
// we start at the start | |
char* cursor = source; | |
// get the first one. | |
// note, strsep replaces the delimiter '|' in the string with a '\0', | |
// so strcpy will stop at the right spot. | |
strcpy(left, strsep(&cursor, "|")); | |
// at this point, cursor will point to the start to the second part, | |
// or NULL if there's nothing left | |
if (!cursor) | |
return; | |
strcpy(right, strsep(&cursor, "|")); | |
printf( | |
"Left: %s\n" | |
"Right: %s\n", | |
left, right | |
); | |
free(source); | |
} | |
int main(void) | |
{ | |
split_in_two("Hello|World"); | |
} |
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 <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
// Microsoft's version of strtok_r is called strtok_s | |
#ifdef _MSC_VER | |
# define strtok_r strtok_s | |
#endif | |
void split_in_two(const char* input) | |
{ | |
// we first make a copy of the input. We don't have to, | |
// but strtok_r operates by overwriting the delimiter in the | |
// input string, and I want to keep it clean. | |
// you can remove that step if you're okay with trashing | |
// the original. | |
char* source = malloc(strlen(input) + 1); | |
if (!source) | |
abort(); | |
strcpy(source, input); | |
char left[50], right[50]; | |
rsize_t length; | |
char* cursor; | |
// get the first one. | |
// note, strtok_r replaces the delimiter '|' in the string with a '\0', | |
// so strcpy will stop at the right spot. | |
strcpy(left, strtok_r(source, "|", &cursor)); | |
// at this point, cursor will point to the start to the second part, | |
// or NULL if there's nothing left | |
if (!cursor) | |
return; | |
// note, we call it with NULL when we're still processing the same string | |
// giving it a non-null input resets it | |
strcpy(right, strtok_r(NULL, "|", &cursor)); | |
printf( | |
"Left: %s\n" | |
"Right: %s\n", | |
left, right | |
); | |
free(source); | |
} | |
int main(void) | |
{ | |
split_in_two("Hello|World"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment