summaryrefslogtreecommitdiff
path: root/hw3/problem2.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw3/problem2.c')
-rw-r--r--hw3/problem2.c32
1 files changed, 32 insertions, 0 deletions
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 <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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]);
+
+}