题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
思路
- 逐串分析,暴力破解
- 根据题目描述,逐步实现加密解密
Answer
#include<stdio.h>
#include<string.h>
int main() {
char str[1000];
char str_un[1000];
scanf("%s", str);
scanf("%s", str_un);
int len = strlen(str);
int len_un = strlen(str_un);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'z') {
str[i] = 'A';
} else{
str[i] = str[i]-32 + 1;
}
}else if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'Z') {
str[i] = 'a';
} else{
str[i] = str[i]+32 + 1;
}
}else if (str[i] >= '0' && str[i] <= '9') {
if (str[i] == '9') {
str[i] = '0';
} else{
str[i] += 1;
}
}
printf("%c", str[i]);
}
printf("\n");
for (int i = 0; i < len_un; i++) {
if (str_un[i] >= 'a' && str_un[i] <= 'z') {
if (str_un[i] == 'a') {
str_un[i] = 'Z';
} else{
str_un[i] = str_un[i]-32 - 1;
}
}else if (str_un[i] >= 'A' && str_un[i] <= 'Z') {
if (str_un[i] == 'A') {
str_un[i] = 'z';
} else{
str_un[i] = str_un[i] +32 - 1;
}
}else if (str_un[i] >= '0' && str_un[i] <= '9') {
if (str_un[i] == '0') {
str_un[i] = '9';
} else{
str_un[i] -= 1;
}
}
printf("%c", str_un[i]);
}
printf("\n");
return 0;
}