summaryrefslogtreecommitdiff
path: root/hw3/problem2.c
blob: 3c345b3bc4ad06c3d0f703aeda9fdc0e41682a3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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]);

}