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>
int main()
{
void encrypt(char *plain, char *cipher);
char p[200],c[200];
gets(p);
encrypt(p,c);
return 0;
}
void encrypt(char *plain, char *cipher)
{
int l;
l=strlen(plain);
int i;
for(i=l-1;i>=0;i--)
{
cipher[i]=plain[i]-24;
printf("%d%d",cipher[i]%10,cipher[i]/10);
}
printf("\n");
}
或者
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 200
int main()
{
void encrypt(char *plain, char *cipher);
char *p,*c;
p=(char *)malloc(N*sizeof(char));
c=(char *)malloc(N*sizeof(char));
gets(p);
encrypt(p,c);
return 0;
}
void encrypt(char *plain, char *cipher)
{
int l;
l=strlen(plain);
int i;
for(i=l-1;i>=0;i--)
{
cipher[i]=plain[i]-24;
printf("%d%d",cipher[i]%10,cipher[i]/10);
}
printf("\n");
}