打印字符数组时显示未知字符
Unknown characters being displayed while printing character array
我正在用 C 语言创建凯撒密码,但在显示编码消息时遇到问题。如果消息包含的字符很少,但一旦消息超过一定数量的字符,printf 函数就会开始显示未知字符,我认为这些字符是垃圾字节。有什么想法吗?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *concat(const char *s1, const char *s2)
{
/*
Remember to free allocated memory
*/
char *result;
if((result = malloc(strlen(s1)+strlen(s2)+1)) != NULL)
{
result[0] = '[=10=]';
strcat(result, s1);
strcat(result, s2);
}
return result;
}
void encode(const char *alpha, const char *message)
{
char result[(sizeof(message) / sizeof(char))];
int x, y, z;
memset(result, '[=10=]', sizeof(result));
for(x = 0; x < strlen(message); x++)
{
for(y = 0; y < 25; y++)
{
if(alpha[y] == message[x])
{
z = (y + 3);
if(z > 25)
{
z = z % 25;
}
result[x] = alpha[z];
}
}
}
printf("%s\n", result);
//return result;
}
int main()
{
char message[200];
char *result;
char array[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
printf("Input your message:");
scanf("%s", &message);
encode(array, message);
}
结果:
改变
char result[(sizeof(message) / sizeof(char))];
至
char result[strlen(message) + 1];
这会在您的缓冲区中为 terminating null-character 留出空间。 sizeof
也不能用于获取 C 字符串的长度,为此我们需要使用 strlen
。您还需要更新 memset
调用。
sizeof
被编译器使用,所以在这种情况下你会问你的编译器 "what is the size of message
",在这种情况下它会是 8(assuimg 64 位),因为指针是 8字节。
除此之外,您应该小心 scanf
,因为您读取了一行输入,所以它现在很好,但是如果您尝试再读取一行,您可能会 run into trouble。
如评论中所述,for(x = 0; x < strlen(message); x++)
每次循环迭代运行一次 strlen
,给您的程序增加相当大的额外负载,随着 message
长度的增加呈指数增长.
您的 concat
函数未被使用,不确定您为什么包含它?
我正在用 C 语言创建凯撒密码,但在显示编码消息时遇到问题。如果消息包含的字符很少,但一旦消息超过一定数量的字符,printf 函数就会开始显示未知字符,我认为这些字符是垃圾字节。有什么想法吗?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *concat(const char *s1, const char *s2)
{
/*
Remember to free allocated memory
*/
char *result;
if((result = malloc(strlen(s1)+strlen(s2)+1)) != NULL)
{
result[0] = '[=10=]';
strcat(result, s1);
strcat(result, s2);
}
return result;
}
void encode(const char *alpha, const char *message)
{
char result[(sizeof(message) / sizeof(char))];
int x, y, z;
memset(result, '[=10=]', sizeof(result));
for(x = 0; x < strlen(message); x++)
{
for(y = 0; y < 25; y++)
{
if(alpha[y] == message[x])
{
z = (y + 3);
if(z > 25)
{
z = z % 25;
}
result[x] = alpha[z];
}
}
}
printf("%s\n", result);
//return result;
}
int main()
{
char message[200];
char *result;
char array[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
printf("Input your message:");
scanf("%s", &message);
encode(array, message);
}
结果:
改变
char result[(sizeof(message) / sizeof(char))];
至
char result[strlen(message) + 1];
这会在您的缓冲区中为 terminating null-character 留出空间。 sizeof
也不能用于获取 C 字符串的长度,为此我们需要使用 strlen
。您还需要更新 memset
调用。
sizeof
被编译器使用,所以在这种情况下你会问你的编译器 "what is the size of message
",在这种情况下它会是 8(assuimg 64 位),因为指针是 8字节。
除此之外,您应该小心 scanf
,因为您读取了一行输入,所以它现在很好,但是如果您尝试再读取一行,您可能会 run into trouble。
如评论中所述,for(x = 0; x < strlen(message); x++)
每次循环迭代运行一次 strlen
,给您的程序增加相当大的额外负载,随着 message
长度的增加呈指数增长.
您的 concat
函数未被使用,不确定您为什么包含它?