c中的strlen错误

时间:2015-12-02 10:29:14

标签: c strlen

<input type="checkbox" id="activation" name="check" disabled/>
  

由于字符与空格分隔,因此strlen将其余部分标记为

     
    垃圾。所以答案总是2.任何想法如何解决?长度是     永远2。         也许它假设空格意味着字符串结尾?         不知道如何修复

  

2 个答案:

答案 0 :(得分:3)

除非您另有说明,否则rand()功能会自动播种,值为1.这就是您每次都得到相同答案的原因。

通过对srand的适当调用进行修复,可能取决于您的系统时钟时间。

strlen不会丢弃空格,但会在达到第一个\0时返回。

答案 1 :(得分:1)

问题在于:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Launch",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:9000/",//Change to whatever you homepage is
        "runtimeArgs": [
            "--new-window", //Open in new window
            "--user-data-dir=C:/dev/", //Can be any directory. Makes chrome load in a different directory so that it opens in a new instance.
            "--remote-debugging-port=9222" //Open in port 9222 (standard chrome debug port)
        ],
        "webRoot": "src/app/", //The directory that contains js, ts and map files
        "sourceMaps": true
    }
  ]
}

token = strtok((words[random]),search); 通过用0终止符替换分隔符来修改其参数;在第一次调用strtok后,strtok变为" c a t ",其长度为2.

您需要在" c"副本上执行strtok调用,如下所示:

words[random]

修改

不要过于苛刻,但......你的代码是一团糟。我不确定你要完成什么,但你有大量未使用的变量,几种类型不匹配,你的输出令人困惑和不可读等等。糟糕的格式化使代码难以理解和遵循;只是使缩进一致暴露了几个问题(例如冗余的char temp[20]; strcpy( temp, words[random] ); ... token = strtok( temp, search ); 调用)。 gcc抛出了一堆关于未使用变量的警告,以及srandsrand的缺失声明。

我已经采用了您的代码,并将其重点放在了randstrtok问题上,并重新格式化了代码和输出,使其具有一定的可读性。结果如下:

strlen

这是输出:

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

int main(void)
{
  int i;
  char words[20][20]={" c a t ",       " c a r ",     " b e a r ",   " s h i p ",   " m o u s e ",
                      " b e a t l e ", " c o a t ",   " n e s t ",   " i c e ",     " s u g a r ",
                      " b a c o n ",   " f r o w n ", " s m i l e ", " d e a d ",   " f e a t h e r ",
                      " g o a t ",     " h e n ",     " j e l l y ", " k o a l a ", " l i p s "};

  char* token;

  const char *search = " ";
  int random;

  srand(time(NULL));

  for( i=0;i<4;i++)
  {
    printf( "%d: ",i );
    random = rand() % 20;
    const char *blanks="                   ";
    printf("%.*s\"%s\", len is %2zu: token list:  ",(int) (strlen( blanks ) - (strlen(words[random]))),
                                                    blanks, words[random], strlen( words[random] ));
    char *sep = "";
    /**
     * strtok modifies the input string by overwriting the delimiter with
     * the 0 string terminator; to preserve the contents of words[random],
     * we need to copy it to a temporary string, and call strtok on that
     * copy.
     */
    char temp[20];                  
    strcpy( temp, words[random] );  
    token = strtok( temp, search ); 
    while(token!=NULL)              
    {
      printf("%s\"%s\"",sep, token);
      sep = ", ";
      token = strtok(NULL, search);
    }
    putchar( '\n' );
  }

  return 0;
}

我不知道您要使用0: " c a r ", len is 7: token list: "c", "a", "r" 1: " b e a t l e ", len is 13: token list: "b", "e", "a", "t", "l", "e" 2: " b e a r ", len is 9: token list: "b", "e", "a", "r" 3: " c a t ", len is 7: token list: "c", "a", "t" list完成什么,但这至少应该有助于解决您在对字符串进行标记时遇到的任何问题。