简体   繁体   中英

How to add words from a string to an array of strings in c

What I have is a string, let's say char input[] = "one two three"; and what I want is a function that takes in two arguments, the input string and an array of strings where I want those words to be.

For example, in pseudo code, transferWords(input, words) would take every word in the input string and put it in the string array words so that words = {"one", "two", "three"} . I can't allocate memory ( malloc() , etc...) to do this since the exercise does not allow me to.

What I've tried is using pointers but this isn't useful because if I happen to access words[21] it would be reading something else:

void transfer(char input[], char *words[20]){

    char *p;
    int i = 0;

    p = strtok(input," \t\n");

    while(p != 0)
    {
        words[i++] = p;
        p = strtok(0, " \t\n");
    }
}

where words would be initalized as char *words[20] = {0}; before.

How could I go about doing this?

(I am still pretty new to C and I'm not very used to it yet, so apologies if this is something obvious.)

If you are not able to resize your arrays, you must allocate them initially with the proper size. For any input array a, the max number of words is (n/2)+1 , where n is the number of characters in a. We then know the max size of any word is n , as we could have an input string with only one word. If you declare your words array with this size, you can guarantee for any input you can capture all the words. You will, in many cases, waste some (or a lot) of space, but you will guarantee all possible words can be stored. I'm not sure how the allocation is done before hand, but see the following code for a general description.

int n;
//Get first the size of the input array...
scanf("%d", n);

//Now we need to get the entire input and allocate our arrays
char input[n + 1]; //plus one for the null terminator if we need it
char words[(n/2) + 1][n + 1]; //(n/2) + 1 max words with a max size of n for each 
//plus one on n for the null terminator

//get input...
fgets(input, n, stdin);

//Now you can run your function

The general more intutive way of doing this is using malloc and realloc to dynamically grow your array so you don't waste so much space, but since you explicitly said you cannot do this, this will work as well and will guarantee the minimum amount of space used while guaranteeing all possible combination of words can be stored.

Then, to move the strings from the input to the words array, use strcpy to copy the individual words to the words array.

void transfer(char *input, char **words){

    char *p;
    int i = 0;

    p = strtok(input," \t\n");

    while(p != NULL)
    {
        strcpy(words[i++], p);
        p = strtok(NULL, " \t\n");
    }
}

As a hint, name your functions and parameters more meaningful.

For Example:

/*
* Breaks the string str into words (delimited by whitespace) 
* and stores them in the array words.
*
* @param str a null-terminated string, must not be NULL
* @param words an array of char pointers, must not be NULL
* @param length the size of the array words, must be >0
*
* @return returns the number of words in the string
*/
int split(char *str, char *words[], unsigned length)
{
    int i=0;

    for (; i < length; ++i, str = NULL) {
        words[i] = strtok(str, "\r\n\t\f ");
        if (words[i] == NULL)
            break;
    }

    return i;
}

int main()
{
    #define N 20
    char *words[N];

    char *input = strdup("one two three");

    int num = split(input, words, N);
    printf("%d\n", num);

    free(input);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM