目录
1171:题目描述
将一段明文加密。
加密的规则如下:将每个字符的ascii码的值减去24作为每个字符加密后的值,例如'a'的ascii码的值为97,那么加密后就变成了73。"73"就是'a'的密文,例如,字符串"abc",在加密之后变为"737475",最后,整个密文再进行翻转,得到最终的密文"574737"。现在请你编写程序,对一段文字加密。请定义并使用如下函数:
void encrypt(char *plain, char *cipher)
{
//把原文字符串plain加密后存入字符串cipher
}
输入
输入一串字符串,只包含数字和字母,最长为200.
输出
输出加密后的字符串。
答案:
#include<stdio.h>
#include<string.h>
void encrypt(char *plain, char *cipher);
int main()
{
char a[201],b[401];
gets(a);
encrypt(a,b);
puts(b);
return 0;
}
void encrypt(char *plain, char *cipher)
{
//把原文字符串plain加密后存入字符串cipher
int i,j;
j=strlen(plain);//j记录plain长度
for(i=j-1,j=0; i>=0; i--)
{
cipher[j++]=(int)(plain[i]-24)%10+'0';//cipher[0]对应plain[最大下标]的个位
cipher[j++]=(int)(plain[i]-24)/10+'0';//cipher[1]对应plain[最大下标]的十位
}
cipher[j]='\0';
}