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