我目前正在尝试简单地用 c 语言加密一个 char 字符串
#include <stdio.h>
#include <string.h>
void encrypt(char *str, int size);
int main(){
char input[8192];
int length;
printf("INPUT A PHRASE: ");
fgets(input, 8192, stdin);
length = strlen(input) -1;
printf("LENGTH: %d\n", length);
char result[length];
strcat(input, result);
encrypt(result, length);
printf("ENCRYPTION: %s\n", result);
return 0;
}
void encrypt(char str[], int size){
int i;
for(i = 0; i < size ; i++){
str[i] = 'X';
}
}
然而,当我去执行代码时,输出会在末尾添加随机字符,如下所示
INPUT A PHRASE: fg
LENGTH: 2
ENCRYPTION: XXà;ÿ
我很困惑为什么要在末尾添加额外的字符。如果有人能澄清为什么这个问题继续发生,我们将不胜感激。
最佳答案
你已经在 strcat 中反转了变量
char *strcat(char *dest, const char *src)
从这里:http://www.tutorialspoint.com/c_standard_library/c_function_strcat.htm
关于c - 输出额外字符的简单字符加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143999/