社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
#include <stdio.h>
#include <string.h>
#define KEY 5 //偏移量
/*
* 加密和解密字符串
* 加密规则:字符串每个字符加上其在字符串中的位置再加上偏移量5成为新字符
*/
char * encrypt(char []);//加密字符串;*代表返回一个指针类型的变量
char * dencrypt(char []);//解密字符串
int main() {
char password[50] = "Hello World!";
encrypt(password);
printf("%sn", password);
dencrypt(password);
printf("%sn", password);
return 0;
}
char * encrypt(char password[]){
int length = strlen(password);//不包括' '
for (int i = 0; i < length; i++) {
password[i] += (i+KEY);
}
return password;
}
char * dencrypt(char password[]){
int length = strlen(password);
for (int i = 0; i < length; ++i) {
password[i] -=(i+KEY);
}
return password;
}
输出:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!