题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <stdio.h>
#include <string.h>
char insecret(char x);
char unsecret(char x);
int main()
{
char willsecret[10001];
char hadsecret[1001];
while (scanf("%s", willsecret) != EOF)
{
scanf("%s", hadsecret);
int len1 = strlen(willsecret);
int len2 = strlen(hadsecret);
for (int i = 0; i < len1; i++)
{
willsecret[i] = insecret(willsecret[i]);
}
for (int i = 0; i < len2; i++)
{
hadsecret[i] = unsecret(hadsecret[i]);
}
printf("%s\n", willsecret);
printf("%s\n", hadsecret);
}
return 0;
}
char insecret(char x)
{
char ret;
if (x >= 'a' && x <= 'z')
ret = (x - 'a' + 1) % 26 + 'A';
else if (x >= 'A' && x <= 'Z')
ret = (x - 'A' + 1) % 26 + 'a';
else if (x >= '0' && x <= '9')
ret = (x - '0' + 1) % 10 + '0';
return ret;
}
char unsecret(char x)
{
char ret;
if (x >= 'a' && x <= 'z')
ret = (x - 'a' - 1 + 26) % 26 + 'A';
else if (x >= 'A' && x <= 'Z')
ret = (x - 'A' - 1 + 26) % 26 + 'a';
else if (x >= '0' && x <= '9')
ret = (x - '0' - 1 + 10) % 10 + '0';
return ret;
}