Domanda

Here is part of my code:

int main ()
{
char *sentence;
char *token;

int counter = 1;
sentence = (char*)malloc(255*sizeof(char));

scanf("%[^\n]s", sentence);
token = strtok(sentence, " ");

char *temp;

while(token != NULL)
{
    printf("Token %d: %s\n", counter, token);
    token = strtok(NULL, " ");
    //temp = token;
    //temp = strtok(NULL, " ");

    counter++;
}
return 0;
}

If I run this when i type: "why herrow there" it gives me:

Token 1: why

Token 2: herrow

Token 3: there

If I uncomment the temp then it only gives me:

Token1: why

Token 2: herrow

It seems even tho I think I didn't hinder my token value with temp, it still affects my token value. I don't want temp to have any affect on my original token. How do I do that?

È stato utile?

Soluzione

You string has three words "why herrow there" The case when you add temp statements:

Step first:

token = strtok(sentence, " ");   <-- sentence: `"why\0herrow there"` 
                                 // token = sentence
char *temp;

first iteration:

while(token != NULL)  // token is not null <-------------------------------+
{                                                                          | 
    printf("Token %d: %s\n", counter, token); // first time print why      |
                                                                           |
    token = strtok(NULL, " ");  <-- sentence: `"why\0herrow\0there"`       |//step-2
                                <-- token points to "herrow" substring  (*)|
    temp = token;               <---temp = token                           |
    temp = strtok(NULL, " ");   <---sentence: `"why\0herrow\0there"`       |//step-3
                                <-- temp = "there" sub string              |//Last token
    counter++;                             |-------------------------------+
}

second iteration of while loop:

while(token != NULL) // token is not null, it is pointing to sustring "herrow"
{
    printf("Token %d: %s\n", counter, token); printing "herrow"
    token = strtok(NULL, " "); <-- no next token, token becomes NULL //step-4
    temp = token;    
    temp = strtok(NULL, " ");  <-- no next token, so temp becomes NULL //step-5

    counter++;
}

Third iteration token is NULL

While loop breaks!

So does it only prints:

Token1: why

Token 2: herrow

Based on comment!

token = strtok(sentence, " "); // first token
next_token = token;
while(next_token != NULL){
    printf("Token %d: %s\n", counter, token);
    if(next_token = strtok(NULL, " "))
           token = next_token;   //Last token in string
    // here you have last token that is not NULL 
    counter++;
}
// next_token is NULL, but token is not NULL it is equals to last token in string
counter--;
printf("Token %d: %s\n", counter, token);

Code working.

Altri suggerimenti

You can achieve what you want by using the reentrant version of strtok() that is strtok_r():

#define _POSIX_SOURCE

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

int main(void)
{ 
  char * sentence = malloc(255 * sizeof(*sentence));

  scanf("%[^\n]s", sentence);

  {
    int counter = 0;
    char * sp1 = NULL;
    char * token = strtok_r(sentence, " ", &sp1);

    while (NULL != token)
    {
      counter++;

      printf("Token %d: %s\n", counter, token);
      token = strtok_r(NULL, " ", &sp1);

      {
        char * sp2 = sp1;
        char * temp = strtok_r(NULL, " ", &sp2);
        if (NULL != temp)
        {
          printf("Temp %d: %s\n", counter, temp);
          temp[strlen(temp)] = ' ';
        }
      }
    }
  }

  return 0;
}

Note: This only work if the set of delimiters passed to strtok() only contains one characters (' ' in this example).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top