Advertisement
codemonkey

Untitled

Oct 11th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. char** strsplit(char* const str, char const delimiter)
  2. {
  3.     int inputlen = strlen(str),
  4.         i,
  5.         wordmaxlen = 20, // To keep track of the current word max length
  6.         wordlen = 0, // To keep track of the current word actual length
  7.         wordnum = 0, // To keep track of the current number of words
  8.         inword = 0, // To separate words
  9.         wordfinished = 0; // To deal with cases where the delimiter is the last
  10.                           // character in the string
  11.  
  12.     // Input validation
  13.     if(inputlen > STRSPLIT_MAX_INPUT_LENGTH)
  14.     {
  15.         errno = STRSPLIT_ERROR_INPUT_TOO_LONG;
  16.         return NULL;
  17.     }
  18.  
  19.     // Allocate the output variable. Assume one word with 20 letters to begin
  20.     // with.
  21.     char** output = NULL;
  22.  
  23.     // Loop through the input string and separate words
  24.     for(i = 0; i < inputlen; i++)
  25.     {
  26.         // If this character is not the delimiter, we're in a word
  27.         if(*(str + i) != delimiter)
  28.         {
  29.             // We're now in a word
  30.             if(!inword)
  31.             {
  32.                 puts("STARTING NEW WORD");
  33.  
  34.                 inword = 1; // Mark that we're now in a word
  35.                 wordmaxlen = 20; // Set or reset the word max length
  36.                 wordnum++; // Increase number of words by 1
  37.                 wordlen = 1; // Set the length of this word to 1
  38.                 wordfinished = 0; // Set this word to not finished
  39.  
  40.                 // Reallocate output to the number of current words. If NULL is
  41.                 // passed as the first argument realloc will behave as malloc.
  42.                 // Remember to make room for a nullbyte
  43.                 output = realloc(output, sizeof(char*) * (wordnum + 1));
  44.  
  45.                 // Allocate room for 20 characters for this new word
  46.                 *(output + (wordnum - 1)) = malloc(sizeof(char) * wordmaxlen);
  47.  
  48.                 // Put this character into the freshly allocated space
  49.                 **(output + (wordnum - 1)) = *(str + i);
  50.             }
  51.             // Continue word
  52.             else
  53.             {
  54.                 // The word is now 1 character longer
  55.                 wordlen++;
  56.  
  57.                 // If we've breached the max length of the word, reallocate ten
  58.                 // more characters. Also remember to compensate for the
  59.                 // required nullbyte
  60.                 if(wordlen > (wordmaxlen - 1))
  61.                     *(output + (wordnum - 1)) =
  62.                         realloc(*(output + (wordnum - 1)), (wordmaxlen += 10));
  63.  
  64.                 // Put the current character into the current char spot in the
  65.                 // current word
  66.                 *(*(output + (wordnum - 1)) + (wordlen - 1)) = *(str + i);
  67.             }
  68.         }
  69.         // Delimiter has been hit! Wrap up the previous word if there is one
  70.         else
  71.         {
  72.             // If we were in a word, end that word here. Otherwise, ignore
  73.             if(inword)
  74.             {
  75.                 puts("ENDING WORD");
  76.  
  77.                 // No longer in a word
  78.                 inword = 0;
  79.  
  80.                 // The nullbyte
  81.                 *(*(output + (wordnum - 1)) + (wordlen)) = '\0';
  82.                
  83.                 // Mark this word as finished
  84.                 wordfinished = 1;
  85.             }
  86.         }
  87.     }
  88.  
  89.     // If the loop finished in the middle of a word, finish that word now
  90.     if(!wordfinished)
  91.     {
  92.         puts("ENDING WORD");
  93.  
  94.         *(*(output + (wordnum - 1)) + (wordlen)) = '\0';
  95.     }
  96.  
  97.     *(output + wordnum) = NULL;
  98.  
  99.     printf("WORD NUMBER %d\n", wordnum);
  100.  
  101.     return output;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement