tags:

views:

340

answers:

3

Hi, I'm writing a small program in C that will read input from the console. Then put it into a char array. After that I need to split the array into words. I'm not sure how to do this. So far I have put the input into a char array. I need to know if there is a way to tokenize based on a blank character. Or any other suggestions on how to handle this problem. Thanks.

sample:

input: this only a test

array: [t,h,i,s, ,i,s, ,o,n,l,y, ,a, ,t,e,s,t,null]

I would like to get a String array [this,is,only,a,test,null]

main() {

    char msg[50], ch;
    int i = 0;

    printf("***Reading words in your input*****\n\n");
    printf("Type something terminated by ENTER button\n");

    while ((ch = getchar()) != '\n')
        msg[i++] = ch;

    msg[i] = '\0';

    i = 0;

    while (msg[i] != '\0')
        putchar(msg[i++]);
    printf("\n");

}
+2  A: 

Yes, use the strtok function:

char* token = strtok(msg, " ");
while (token != NULL) {
  printf("%s", token);
  token = strtok(NULL, " ");
}
JG
One thing to beware of is that strtok() maintains a static state, so it is not thread-safe. Use strtok_r() if that's an issue.
Fred Larson
strtok() changes the original string. It might be a problem too; you cannot strtok() a string literal :)
pmg
If that's really a problem, you can just `strcopy` the original string.
JG
A: 

If you want to read all the words until the end of file (not stopping at newline), then this is simpler:

#include <stdio.h>
int main(){
    char b[999];
    while (scanf("%s",b)==1)
        puts(b);
    return 0;
}

The return value of scanf is the number of fields parsed succesfully, or EOF. Whitespace serves to separate "%s" fields, so you get the tokens you want. Of course, if you do read a line first, you can still use sscanf.

If you want to accumulate an array of strings instead of just processing them one by one ("puts(b)" above), then you'll need to get your hands dirty with realloc,malloc,strcpy, and strlen. Fixed sized buffers are nasty anyway.

wrang-wrang
A: 

C doesn't have strings in the sense you use the word. C has arrays of characters.

If you want an array of strings ... you can have something similar as an array of arrays of chars.

But each array has a pre-determined size, and you can't add more "strings" or make them longer than the space available. The other option (instead of arrays of arrays of char) is to use pointers and memory from malloc() and friends (I'll leave the discussion of poonters, malloc() and friends for another time).

To define an array of array of char and split a sentence by the spaces, you can do this

char array_of_string[10][6]; /* 10 strings of a maximum of 5 characters each, plus the NUL */
char msg[50] = "this is a test";
/* now split the msg and put the words in array_of_string */
int string_num = 0;
int word_size = 0;
int msg_index = 0;

while (msg[msg_index] != '\0') {
    if (msg[msg_index] != ' ') {
        /* add the character to the proper place in array_of_string */
        array_of_string[string_num][word_size] = msg[msg_index];
        /* and update word_size for next time through the loop */
        word_size++; /* needs check for reserved word size (5) */
    } else {
        /* a space! */
        /* first, terminate the current word */
        array_of_string[string_num][word_size] = '\0';
        /* prepare to start the next word */
        string_num++; /* needs check for reserved number of "strings" (10) */
        word_size = 0;
    }
}
/* array_of_string[0] is now "this"
 * array_of_string[1] is now "is"
 * ...
 */
pmg