Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中的Strlen错误 #包括 #包括 #包括 字符*矩阵[10][10]; int main(void){ int i; 字符*列表[4]; 字符词[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 l y”、“k o a l a l a l a”、“l p s”}; 整数长度=0; int-num; int k; int m; char otherString=0; 字符c; int j; int-s; INTR; 煤焦试验[10]; 字符*令牌; const char*search=“”; char*empty=“”; 整数大小; INTANS; int x; int-y; int pos; int-pos2; 兰德罗国际酒店; 内柱; 选择整数[10]; int随机; int d; int ROWS=10;//行数 int COLUMNS=10;//列数 printf(“\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n”); srand(时间(空)); 对于(r=0;r_C_Strlen - Fatal编程技术网

c语言中的Strlen错误 #包括 #包括 #包括 字符*矩阵[10][10]; int main(void){ int i; 字符*列表[4]; 字符词[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 l y”、“k o a l a l a l a”、“l p s”}; 整数长度=0; int-num; int k; int m; char otherString=0; 字符c; int j; int-s; INTR; 煤焦试验[10]; 字符*令牌; const char*search=“”; char*empty=“”; 整数大小; INTANS; int x; int-y; int pos; int-pos2; 兰德罗国际酒店; 内柱; 选择整数[10]; int随机; int d; int ROWS=10;//行数 int COLUMNS=10;//列数 printf(“\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n”); srand(时间(空)); 对于(r=0;r

c语言中的Strlen错误 #包括 #包括 #包括 字符*矩阵[10][10]; int main(void){ int i; 字符*列表[4]; 字符词[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 l y”、“k o a l a l a l a”、“l p s”}; 整数长度=0; int-num; int k; int m; char otherString=0; 字符c; int j; int-s; INTR; 煤焦试验[10]; 字符*令牌; const char*search=“”; char*empty=“”; 整数大小; INTANS; int x; int-y; int pos; int-pos2; 兰德罗国际酒店; 内柱; 选择整数[10]; int随机; int d; int ROWS=10;//行数 int COLUMNS=10;//列数 printf(“\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n”); srand(时间(空)); 对于(r=0;r,c,strlen,C,Strlen,,除非您另有说明,否则,rand()函数会自动添加值为1的种子。这就是为什么每次都会得到相同的答案 通过适当调用srand进行修复,可能基于系统时钟时间 strlen不会丢弃空格,而是在到达第一个\0时返回。问题在于: #include <stdio.h> #include <time.h> #include <string.h> char* matrix [10][10]; int main(void){ int i;

,除非您另有说明,否则,
rand()
函数会自动添加值为1的种子。这就是为什么每次都会得到相同的答案

通过适当调用
srand
进行修复,可能基于系统时钟时间

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

问题在于:

 #include <stdio.h>
#include <time.h>
#include <string.h>
char* matrix [10][10];  
int main(void){         
        int i;
          char* list[4];
         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 "};

        int length=0;
        int num;
        int k;
        int m;
        char otherString=0;
        char c;
        int j;
        int s;
        int r;


        char test[10];
        char* token;

const char *search = " ";
        char* empty="";
        int size;
        int ans;

        int x;
        int y;
        int pos;
        int pos2;
        int randRow;
        int randColumn;
        int chosen[10];
        int random;
        int d;
        int ROWS      = 10;      // number of rows
 int COLUMNS   = 10;      // number of columns




        printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");


        srand(time(NULL));


            for(r=0;r<10;r++){

            for(s=0;s<10;s++){
srand(time(NULL));

            c='1';  


        memset(matrix,c,sizeof(matrix));        
            }}


        for( i=0;i<4;i++){
    printf( "\n%d",i );
    d=0;
        do{
            random = (rand()%20);
            list[i]=words[random];
            d=0;
            for( j=0;j<i;j++){
                if(strcmp(words[random],list[j])==0)d=1;
            }
        }while(d);


    printf("%s",words[random]);
    token = strtok((words[random]),search); 
    printf("%s",token);
    while(token!=NULL) 
   {
      printf("\nrand%d",random);
      length=strlen(words[(random)]);
     printf("len is:%d",length);
      matrix [i][0]=token;
          for( k=1;k<=length;k++){
          token = strtok(NULL, search); 
        printf("%s",token);

            matrix [i][k]=token;

      } 
    } }


            for(r=0;r<10;r++){
            printf("\n");   
            for(s=0;s<10;s++){




                 printf("\t%c",matrix[r][s]);


            }}


        getchar();
            return 0;
            }
strtok
通过将分隔符替换为0终止符来修改其参数;在第一次调用
strtok
后,
的“c a t”
变为
的“c”
,其长度为2

您需要对
words[random]
的副本执行
strtok
调用,如下所示:

token = strtok((words[random]),search); 
编辑

不要过分苛刻,但是……你的代码乱七八糟。我不确定你想完成什么,但你有大量未使用的变量,几个类型不匹配,输出混乱和不可读,等等。糟糕的格式使代码难以理解和遵循;只是使缩进一致暴露了几个问题ues(例如冗余的
srand
调用)。gcc抛出了一系列关于未使用变量的警告,以及
srand
rand
的声明缺失

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

char temp[20];
strcpy( temp, words[random] );
...
token = strtok( temp, search );

我不知道你想用
列表
矩阵
完成什么,但这至少有助于解决字符串标记化的问题。

strlen函数查找终止的空字符
'\0'
,它不关心空格。你是如何显示/使用的de>length?您是否也显示所选单词以验证您在有效字符串上调用
strlen
?是否包含
string.h
?这将有助于查看更多上下文。您的结论不正确,并且您声称的结果没有意义。请提供一个示例,以便我们能够准确地了解您的原意这样我们就可以重现你的结果。你的代码格式/缩进…不寻常。为什么?我的代码有问题吗?即使是相同的rand()每次值时,我都看不到该数组中任何长度为2的元素?除非OP打印的是
随机
,而不是
长度
。这是我的猜测。但我无法判断该问题当前的编写方式。我只是通过将OPs代码复制粘贴到一个空项目中,然后输出val来运行一个快速测试长度和单词的ue[random]。它准确地给出了单词的长度,但正如这个答案正确说明的那样,由于播种,它将始终给出相同的答案。在我的例子中,random的值为2,这给出了答案“c a r”,长度为7。我正在打印长度,@chris你能编辑你的OP来显示你用来打印长度的代码吗?以及任何其他相关代码吗?由于
rand
函数产生相同的值,你现在总是会得到相同的单词。如我在评论中所述,在你的OP an中使用准确的代码d添加
std::coutstrlen每次仍然给出2:((
#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"