Skip to content

Instantly share code, notes, and snippets.

@castaneai
Created May 21, 2014 13:14
Show Gist options
  • Save castaneai/69e3e60f9d667be8d39c to your computer and use it in GitHub Desktop.
Save castaneai/69e3e60f9d667be8d39c to your computer and use it in GitHub Desktop.
split関数のテスト
#include <stdio.h>
#include <memory.h>
#include <string.h>
char** split(char* originalString, int stringNum)
{
int i;
char** stringArray;
char* tp;
stringArray = (char**) malloc(stringNum * sizeof(char*));
for (i = 0; i < stringNum; i++) {
stringArray[i] = (char*) malloc(1024 * sizeof(char));
}
i = 0;
tp = strtok(originalString, ",");
stringArray[i] = tp;
i++;
while (tp != NULL) {
tp = strtok(NULL, ",");
if (tp != NULL) {
stringArray[i] = tp;
i++;
}
}
return stringArray;
}
int main(void)
{
int i;
char input [] = "is,this,the,zombie?";
char** arr = split(input, 4);
for (i = 0; i < 4; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment