gpt4 book ai didi

c - 输出中存在未知字符

转载 作者:行者123 更新时间:2023-11-30 19:49:04 24 4
gpt4 key购买 nike

当接受用户输入时,我有时会得到未知字符:

http://imageshack.us/photo/my-images/833/capture1ec.jpg/

这是我的代码:

#include <stdio.h>

void main()
{
char plaintext[50];
char arr[10]={'a','b','c','d','e','f','g','h','i','j'};
char key[20];
int plain=0,max1=0,max2=0;
char chip[30][30];
int z=0,i,j,k=0,c=0,a;
char swap,temp;
printf("Enter key :");
scanf("%[^\n]",key);

for(i=0;key[i]!='\0';i++)
{
max2++;
}

printf("Enter plaintext :");
scanf("%s",&plaintext);

for( i = 0; plaintext[i] != '\0'; i++ )
{
plain++;
}

if (plain%max2==0)
max1=plain/max2;
else
max1=plain/max2+1;

while (plaintext[k]!='\0')
{
for (i=0;i<max1;i++)
{
for (j=0;j<max2;j++)
{
if (plaintext[k]!=NULL)
{
chip[i][j]=plaintext[k];
k++;
}
}
}
}

/*printf("\n");
for (i=0;i<max1;i++)
{
for (j=0;j<max2;j++)
{
printf("%c ",chip[i][j]);
}
printf("\n");
}*/

for(i=0;i<max2;i++)
{
if(chip[max1-1][i]==NULL)
{
chip[max1-1][i]=arr[c];
c++;
}
}

printf("\n");
/*for (i=0;i<max1;i++)
{
for (j=0;j<max2;j++)
{
printf("%c ",chip[i][j]);
}
printf("\n");
}*/

for (i = 0 ; i < max2; i++)
{
for (j = 0 ; j < max2 ; j++)
{
if (key[i] < key[j])
{
swap = key[j];
key[j] = key[i];
key[i] = swap;

for (a=0;a<max1;a++)
{
temp=chip[a][j];
chip[a][j]=chip[a][i];
chip[a][i]=temp;
}
}
}
}

/*printf("\n");
for (i=0;i<max1;i++)
{
for (j=0;j<max2;j++)
{
printf("%c ",chip[i][j]);
}
printf("\n");
}*/
for (i=0;i<max2;i++)
{
for (j=0;j<max1;j++)
{
if (chip[j][i]!=NULL)
printf("%c ",chip[j][i]);
}
}
}

最佳答案

尝试在使用芯片之前添加此内容。

#include <string.h>

...

char chip[30][30];
memset(chip, 0, 30 * 30); // <- add this one

...

或者简单地

char chip[30][30] = {0};

关于c - 输出中存在未知字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755619/

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