Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void) {
- char * buf = malloc(10000*sizeof(char));
- buf = strcpy(buf,"This is some sample input from the user \n\0");
- char ** wordArr;
- char * tokenWord;
- char * tokens = " -,.";
- int i = 0;
- int j = 0;
- int wordCount = 0;
- // Get number of individual words in buffer.
- for (i = 0; i < (int)strlen(buf); i++) {
- if(((buf[i] == ' ') || (buf[i] == '\n')) && (buf[i-1] != ' ')) {
- wordCount++;
- }
- }
- int initial_size = 1;
- wordArr = malloc(initial_size * sizeof(char *));
- // Good until here:
- tokenWord = strtok(buf,tokens);
- i = 0;
- while(tokenWord != NULL) {
- printf("tokenWord: %s\n", tokenWord);
- wordArr[i] = tokenWord;
- printf("wordArr[i]: %s\n",wordArr[i]);
- strcat(wordArr[i],"\0");
- initial_size++;
- wordArr = realloc(wordArr, sizeof(char *) * initial_size);
- tokenWord = strtok(NULL,tokens);
- printf("made it here loop iteration: %d\n",i);
- i++;
- }
- printf("made it here 2\n");
- i = 0;
- j = 0;
- printf("%s\n",wordArr[2]);
- return 1;
- }
Add Comment
Please, Sign In to add comment