Skip to content

Instantly share code, notes, and snippets.

@FurryHead
Created July 13, 2011 20:43
Show Gist options
  • Save FurryHead/1081275 to your computer and use it in GitHub Desktop.
Save FurryHead/1081275 to your computer and use it in GitHub Desktop.
C split/join functions, using strtok_r -- Main test
#include <stdio.h>
#include <stdlib.h>
#include "string_sj.h"
int main(int argc, char** argv) {
char **items = NULL;
const char *s = "Hello, I'm splitting on the space delimiter!";
const char *delim = " ";
char *tmp;
int i;
int num_items = str_split(s, delim, &items);
if (num_items == -1) {
printf("Sorry, str_split failed.");
return EXIT_FAILURE;
}
printf("Original: %s\n", s);
printf("Split items: \n");
for (i = 0; i < num_items; i++)
printf("%d: %s\n", i, items[i]);
printf("Now, what does str_join() have to say about items?\n");
tmp = str_join(items, 0, num_items, " ");
if (tmp == NULL) {
printf("str_join() failed.");
free(items);
return EXIT_FAILURE;
}
printf("str_join() says: %s\n", tmp);
printf("The end. :) \n");
free(tmp);
free(items);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment