【发布时间】:2021-01-21 18:10:42
【问题描述】:
#include <iostream>
#include <string.h>
using namespace std;
void crypt(char* sMsg)
{
cout << "Original Message: '" << sMsg << "'" << endl;
int length = strlen(sMsg);
char sMsg_Crypt[3][length];
/* sMsg_Cryp[3]
[0] CRYPT LETTERS, ASCII + 3
[1] INVERT CHAR
[2] HALF+ OF SENTENCE, ASCII - 1
*/
for (int i=0; i<length; i++)
{
if (isalpha((int)sMsg[i]))
sMsg_Crypt[0][i] = sMsg[i] + 3; // DO ASCII + 3
else
sMsg_Crypt[0][i] = sMsg[i];
}
cout << "Crypt[0]: '" << sMsg_Crypt[0] << "'" << endl;
}
int main()
{
char sMsg[256];
cin.getline(sMsg,256);
crypt(sMsg);
return 0;
}
输入:
世界你好!测试密码学...
输出:
原始消息:'Hello World!测试密码学...'
地穴[0]:'Khoor Zruog! Whvwlqj wkh Fu|swrjudsk|...Çio'
为什么会出现这个Çio??
【问题讨论】:
-
因为您的字符数组不是以空值结尾的。而且它们也是非标准C++,最好使用
std::string。 -
你的空终止符在哪里?
-
欢迎来到 Stack Overflow。请参阅How to Ask。另见Minimal Reproducible Example