0

My program takes in a string and I break the string into words based on a space, I store the words in an array of pointers. However for some reason it is not separating the words into the appropriate index. In this example and the picture below the text "suspend 0", token1 should correspond to 0 however it corresponds to "end"

int main(){
    int ch ,n= 1;
    int i = 0;
    char str[512], *token[5], *act_token;
    while(1){

            printf("Enter text: ");
            while((ch = getchar()) != '\n')
                    str[i++] = ch;
            str[i] = '\0';
            i = 0;

            printf("string: %s\n", str);

            int spaces = 0;
            for(int counter  = 0; counter < strlen(str) + 1; counter++){
                    if(str[counter] == ' '){
                            spaces++;
                    }
            }
            printf("Spaces: %d\n", spaces);
            strtok(str, " ");
            while(n <= spaces && (act_token = strtok(NULL, " "))){
                    token[n] = act_token;
                    n++;

            }
            token[n] = NULL;
    //      printf("token[1]: %s\n", token[1]);
            for(int x = 1; x < spaces+1; x++){
                    printf("token[%d]: %s\n", x, token[x]);

            }
    }
    return 0;

}

1 Answer 1

1

In this example and the picture below the text "suspend 0", token1 should correspond to 0 however it corresponds to "end"

That'a because (most likely) your index n, which is for some reason initiated to 1 (why not 0?) outside the while loop is not reset at the end of each while loop iteration...

Try this:

        while(n <= spaces && (act_token = strtok(NULL, " "))){
                token[n] = act_token;
                n++;

        }
        token[n] = NULL;
        n = 1; <-- *** add this line ***
//      printf("token[1]: %s\n", token[1]);
        for(int x = 1; x < spaces+1; x++){
                printf("token[%d]: %s\n", x, token[x]);

        }

NOTE1: You could eliminate all those search loops if you checked each char during the input process inside while((ch = getchar()) != '\n'). Anyway you collect the input char by char, so check it's value and count the spaces + split the words.

NOTE2: You are creating arrays of fixed size (for which is better to use defines) but you never check for the input not to exceed the limits of those arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

I set n =1 becasue I don't want the first word. This program is just a samller version of a bigger version I'm working on
@pennyBoy Did setting n=1 inside the while loop (as I wrote in the answer) helped resolving this issue? (Also see my recent notes in the answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.