1

这是我的代码的一部分:

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;
}

如果我在输入时运行它:“为什么她在那里”,它会给我:

Token 1: why

Token 2: herrow

Token 3: there

如果我取消注释 temp 那么它只会给我:

Token1: why

Token 2: herrow

即使我认为我没有用 temp 阻碍我的代币价值,它仍然会影响我的代币价值。我不希望 temp 对我的原始令牌产生任何影响。我怎么做?

4

2 回答 2

1

您的字符串有三个单词"why herrow there"添加temp语句时的情况:

第一步:

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

第一次迭代:

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++;                             |-------------------------------+
}

while循环的第二次迭代:

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++;
}

第三次迭代令牌为 NULL

虽然循环中断!

所以它只打印:

Token1: why

Token 2: herrow

根据评论!

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);

代码工作

于 2013-09-29T05:10:10.207 回答
0

您可以通过使用可重入版本来实现您想要strtok()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;
}

注意:这仅在传递给的分隔符集strtok()仅包含一个字符时才有效(' '在本例中)。

于 2013-09-29T10:11:55.067 回答