gpt4 book ai didi

c - 获取字符串c

转载 作者:行者123 更新时间:2023-11-30 17:07:20 24 4
gpt4 key购买 nike

这是一个简单的字谜游戏代码:

#define NP 5
#define LP 10
void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;

a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
merda[j] = '\0';
for (i = 0; i < len; i++){
if (merda[i] =! yoda.parola[i]) cont++;
printf("\n%d", cont);
printf("\n%c", merda[i]);
printf("\n%c", yoda.parola[i]);
}
if (cont =! 0) printf("\nFALSE");
else printf ("\nTRUE");
}

程序运行,但是当我插入单词并将其与原始单词进行比较时,即使我输入了正确的单词,最终输出也始终为 FALSE。没有\0 也会出现问题。有人可以帮我吗?

感谢您的回答,这是一个完美运行的程序:

void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;

a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
for (i = 0; i < len; i++){
if (merda[i] != yoda.parola[i]) cont++;

}
i = strlen(merda);
if (cont != 0 || i > len ) printf("\nNON HAI INDOVINATO");
else printf ("\nHAI INDOVINATO");

}

最佳答案

(cont =!0) 应该是 (cont != 0) – 凯勒姆

关于c - 获取字符串c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com