Guest User

Untitled

a guest
Nov 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. int main(void) {
  7.   char * buf = malloc(10000*sizeof(char));
  8.   buf = strcpy(buf,"This is some sample input from the user \n\0");
  9.   char ** wordArr;
  10.   char * tokenWord;
  11.   char * tokens = " -,.";
  12.   int i = 0;
  13.   int j = 0;
  14.   int wordCount = 0;
  15.   // Get number of individual words in buffer.
  16.   for (i = 0; i < (int)strlen(buf); i++) {
  17.     if(((buf[i] == ' ') || (buf[i] == '\n')) && (buf[i-1] != ' ')) {
  18.       wordCount++;
  19.     }
  20.   }
  21.   int initial_size = 1;
  22.   wordArr = malloc(initial_size * sizeof(char *));
  23. // Good until here:  
  24.   tokenWord = strtok(buf,tokens);
  25.   i = 0;
  26.   while(tokenWord != NULL) {
  27.     printf("tokenWord: %s\n", tokenWord);
  28.     wordArr[i] = tokenWord;
  29.     printf("wordArr[i]: %s\n",wordArr[i]);
  30.     strcat(wordArr[i],"\0");
  31.     initial_size++;
  32.     wordArr = realloc(wordArr, sizeof(char *) * initial_size);
  33.     tokenWord = strtok(NULL,tokens);
  34.     printf("made it here loop iteration: %d\n",i);
  35.     i++;
  36.   }
  37.   printf("made it here 2\n");
  38.   i = 0;
  39.   j = 0;
  40.   printf("%s\n",wordArr[2]);
  41.   return 1;
  42. }
Add Comment
Please, Sign In to add comment