From 9fae5d516012e2c0802105e67c79e2587a22b9dc Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 1 Jan 2020 22:37:37 -0500 Subject: Inital commit --- hw3/problem2.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 hw3/problem2.c (limited to 'hw3/problem2.c') diff --git a/hw3/problem2.c b/hw3/problem2.c new file mode 100644 index 0000000..3c345b3 --- /dev/null +++ b/hw3/problem2.c @@ -0,0 +1,32 @@ +#include +#include +#include + +int string2array(char *inString, char **wordarray){ + const char whitespace[] = " "; + char **cursor = wordarray; + int ret = 0; + char *this = NULL; + while(1){ + this = strtok(inString,whitespace); + if(this == NULL) + break; + char *dest = strcpy(wordarray[ret],this); + inString = NULL; + ret++; + } + return ret; +} + +int main(){ + char instring[] = "This is a string that needs to be split"; + char **wordarray = (char**) malloc(sizeof(char*) * 20); + for(int i = 0; i < 20; i++){ + wordarray[i] = (char*)malloc(sizeof(char) * 100); + } + string2array(instring,(char**)wordarray); + printf("wordarray ended up being:\n"); + for(int i = 0; i < 20; i++) + printf("%d: %s\n",i,wordarray[i]); + +} -- cgit v1.2.3-70-g09d2