题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
大小写字母相差32即0x20,按位异或即可转换
int main() { char str[1001]; scanf("%s", str); for(int i=0; i<strlen(str); i++) { if(str[i]=='Z') str[i] = 'a'; else if(str[i]=='z') str[i] = 'A'; else if(str[i]=='9') str[i] = '0'; else if(str[i]>='0' && str[i]<='8') str[i] += 1; else str[i] = (str[i]^0x20) + 1;//大小写相差32即0x20,按位异或即可转换 } printf("%s\n", str); scanf("%s", str); for(int i=0; i<strlen(str); i++) { if(str[i]=='a') str[i] = 'Z'; else if(str[i]=='A') str[i] = 'z'; else if(str[i]=='0') str[i] = '9'; else if(str[i]>='1' && str[i]<='9') str[i] -= 1; else str[i] = (str[i]^0x20) - 1; } printf("%s\n", str); }