1171: 加密(指针专题)
题目描述
将一段明文加密。加密的规则如下:将每个字符的ascii码的值减去24作为每个字符加密后的值,例如’a’的ascii码的值为97,那么加密后就变成了73。“73"就是’a’的密文,例如,字符串"abc”,在加密之后变为"737475",最后,整个密文再进行翻转,得到最终的密文"574737"。现在请你编写程序,对一段文字加密。请定义并使用如下函数:
void encrypt(char *plain, char *cipher)
{
//把原文字符串plain加密后存入字符串cipher
}
输入
输入一串字符串,只包含数字和字母,最长为200.
输出
输出加密后的字符串。
样例输入 Copy
zero12
样例输出 Copy
625278097789
提示
直接输出密文当然是最简单的方法,不过本题要求你将密文存入字符串(以备程序的其它模块使用)。
来源/分类
#include<stdio.h>
#include<string.h>
#define N 202
/*
数字和字母的ASCII码-24后都是2位数
*/
void encrypt(char *plain,char *cipher){
int j=0,len;
len=strlen(plain);
for(int i=len-1;i>=0;i--){
cipher[j++]=(int)(plain[i]-24)%10+'0';
cipher[j++]=(int)(plain[i]-24)/10+'0';
}
cipher[j]='\0';
}
int main(){
char *p,*q;
p=(char *)malloc(sizeof(char)*N);
q=(char *)malloc(sizeof(char)*(2*N));
gets(p);
encrypt(p,q);
puts(q);
return 0;
}