gpt4 book ai didi

字符*字; printf ("%s",字)问题

转载 作者:行者123 更新时间:2023-11-30 18:37:57 33 4
gpt4 key购买 nike

StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将每个字母替换为 '*' 并在lenght+1位置放置一个'\0'来结束字符串。

我在获取 char* 的大小时遇到​​问题,最大的问题是我无法在控制台上正确显示它,我只打印奇怪的符号。

这是我的代码

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

void pendu()
{
FILE *fichier = NULL;
fichier = fopen("dico.txt", "r");
if (fichier == NULL)
{
printf("Erreur ouverture du fichier");
}
int taille = get_size("dico.txt");

int nb;
srand(time(NULL));
nb = rand() % taille;

char *momo = get_word(nb, fichier);

printf("Vous allez jouer au pendu! \n");
printf("Voici le mot a trouver : \n");
int longueur = sizeof(momo); // i know size of return size in byte so may i
//do sizeof(word)/sizeof(char) ?

char *motcrypt = cryptage(momo,longueur);

printf(" %s \n", motcrypt); // here is the main Problem.
int tour = 10;
while (tour > 0)
{
char lettre;

printf(" %s \n", motcrypt);
printf("il vous reste %d tour(s) \n", tour);

printf("proposer une lettre :", tour);

scanf(" %c", &lettre); // space needed !!?!!

printf("\n");
char *motcrypt2 = motcrypt;
for (int i = 0; i < longueur; i++)
{
if (lettre == momo[i])
{
motcrypt2[i] = lettre;
}
}
if (motcrypt == motcrypt2)
{
tour = tour-1;
printf("%s", motcrypt);
}
if (motcrypt2 == momo)
{
motcrypt = motcrypt2;
printf("%s", motcrypt);
printf("Vous avez gagner! \n");
printf("Appuyer sur un bouton pour rejouer \n");
system("Pause");

}
else if (tour == 0)
{
printf("Vous avez perdu\n");
printf("Appuyer sur un bouton pour rejouer \n");
system("Pause");
pendu();
}
}

}

void main()
{
pendu();
}

编辑:这是我的“cryptage”函数:

char* cryptage(char *yala,int x)
{
int i = 0;
char crypted[30];
while (i < x)
{
crypted[i] = '*';
crypted[i + 1] = '\0';
i++;
}
return crypted;
}

我得到了我的加密函数,它将每个字母替换为“*”,并在长度+1位置放置一个“\0”来结束字符串。

PS:我希望有人能解释一下为什么 printf("%s",word) 的输出与 printf("%s",word)

最佳答案

您的代码存在几个问题,但让我们从您的 cryptage 函数开始:

char* cryptage(char *yala,int x)
{
int i = 0;
char crypted[30];
while (i < x)
{
crypted[i] = '*';
crypted[i + 1] = '\0';
i++;
}
return crypted;
}

除了错误的缩进之外,您还返回一个指向具有自动存储持续时间的数组的指针,该指针在函数返回时超出范围。使用该指针将导致未定义的行为(包括潜在的崩溃或奇怪的输出,或者偶尔看起来正常工作)。您应该为 crypted 动态分配存储(查找 malloc),或者接受指向它的指针作为参数:

char* cryptage(char *yala, int x, char *crypted)
{
int i = 0;
while (i < x)
{
crypted[i] = '*';
crypted[i + 1] = '\0';
i++;
}
return crypted;
}

这一行:

int longueur = sizeof(momo); // i know size of return size in byte so may i

...大概是为了获取momo指向的字符串的长度,而不是指针本身的大小。因此,它应该是:

size_t longueur = strlen(momo); // i know size of return size in byte so may i

几个 if 语句似乎正在尝试比较字符串:

    if (motcrypt == motcrypt2)

...但这是比较指针,而不是字符串内容。您需要使用strcmp函数:

    if (strcmp(motcrypt,motcrypt2) == 0)

您尚未显示各种其他方法的定义,因此不可能为您提供完整的问题列表。当您发布到 StackOverflow 时,您应该发布一个显示问题的完整(但最小)示例(请参阅 MCVE )。

关于字符*字; printf ("%s",字)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361303/

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