我正在尝试加密用户根据我已经定义的2个字符串写的内容
该计划运作良好
但是当用户写超过30个字符时我遇到了问题
在加密结束时,程序会出现随机字符
#include<stdio.h>
#include<string.h>
main()
{
char read [120] ;
char Encryption [55] = {'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','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'};
char Decryption [55] = {'k','K','f','z','L','S','j','T','v','u','B','F','q','U','P','h','n','M','i','Z','w','x','y','Y','W','t','m','s','N','J','Q','E','G','l','X','a','A','c','b','C','e','o','D','H','I','r','d','R','V','p','p','g'};
char new[120];
int i , k;
gets(read);
for (i=0 ; i<strlen(read) ; i++)
{
for(k=0 ; k<strlen(Encryption) ; k++){
if (read[i]==' ') new[i]=' ';
else if (read[i] == Encryption[k]){
new[i]=Decryption[k];}
}
}
puts(new);
getch();
}
例如当用户写(((ALNOR ALNOR)))输出为(((kFUPM kFUPM)))
但是当我写(((ALNOR ALNOR ALNOR ALNOR ALNOR)))输出应该是(((kFUPM kFUPM kFUPM kFUPM kFUPM)))但是apears是什么(((kFUPM kFUPM kFUPM kFUPM kFUPM H&amp; M) ))
我不会从H&amp; M来到哪里,当我再次运行H&amp; M chaing to other else
??? !!!
答案 0 :(得分:0)
As&#39; Drax&#39;表示,代码需要正确终止“新”&#39;串。也许在for循环之后插入一行:
for (i=0 ; i<strlen(read) ; i++)
{
for(k=0 ; k<strlen(Encryption) ; k++){
if (read[i]==' ') new[i]=' ';
else if (read[i] == Encryption[k]){
new[i]=Decryption[k];}
}
}
}
new[i] = '\0';