0

I am trying to split a line into an array of words, but I am stuck on how to do this in C. My skills in C aren't very good, so I can't think of a way to "execute" my idea. Her is what I have so far:

int beginIndex = 0;
int endIndex = 0;
int maxWords = 10;
while (1) {
   while (!isspace(str)) {
      endIndex++;
   }
   char *tmp = (string from 'str' from beginIndex to endIndex)
   arr[wordCnt] = tmp;
   wordCnt++;
   beginIndex = endIndex;
   if (wordCnt = maxWords) {
       return;
   }
}

In my method I receive (char *str, char *arr[10]), and str is the line that I want to split when I encounter a space. arr is the array where I want to store the words. Is there any way to copy the 'chunk' of string that I want from 'str' into my tmp variable? This is the best way that I can think of right now, perhaps it's a terrible idea. If so, I would be happy to get some documentation or tips on a better method.

user16655
  • 1,901
  • 6
  • 36
  • 60
  • 2
    You can use strtok to split string into words – radar Oct 12 '14 at 21:36
  • What's your problem? – CroCo Oct 12 '14 at 21:38
  • Thank you! strtok worked on the first try :) But, I am calling this method with a line from main, and I know I need to allocate memory somewhere. Should I allocate memory to my char arr[10] before I send it to this method from main? – user16655 Oct 12 '14 at 21:50
  • possible duplicate of [How do I tokenize a string in C++?](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – Jared Burrows Oct 12 '14 at 21:57

2 Answers2

2

You should check out the C Library function strtok. You simply feed it the string you want to break up and a string of delimiters.

Here is an example of how it works (taken from the linked site):

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");

  while (pch != NULL) {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }

  return 0;
}

In your case instead of printing each string you would assign the pointer returned by strtok to the next element in your array arr.

cubiclewar
  • 1,569
  • 3
  • 20
  • 37
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int split(char *str, char *arr[10]){
    int beginIndex = 0;
    int endIndex;
    int maxWords = 10;
    int wordCnt = 0;

    while(1){
        while(isspace(str[beginIndex])){
            ++beginIndex;
        }
        if(str[beginIndex] == '\0')
            break;
        endIndex = beginIndex;
        while (str[endIndex] && !isspace(str[endIndex])){
            ++endIndex;
        }
        int len = endIndex - beginIndex;
        char *tmp = calloc(len + 1, sizeof(char));
        memcpy(tmp, &str[beginIndex], len);
        arr[wordCnt++] = tmp;
        beginIndex = endIndex;
        if (wordCnt == maxWords)
            break;
    }
    return wordCnt;
}

int main(void) {
    char *arr[10];
    int i;
    int n = split("1st 2nd 3rd", arr);

    for(i = 0; i < n; ++i){
        puts(arr[i]);
        free(arr[i]);
    }

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70