题目1105:字符串的反码

题目描述:

    一个二进制数,将其每一位取反,称之为这个数的反码。下面我们定义一个字符的反码。如果这是一个小写字符,则它和字符'a’的距离与它的反码和字符'z’的距离相同;如果是一个大写字符,则它和字符'A’的距离与它的反码和字符'Z’的距离相同;如果不是上面两种情况,它的反码就是它自身。

    举几个例子,'a’的反码是'z’;'c’的反码是'x’;'W’的反码是'D’;'1’的反码还是'1’;'$'的反码还是'$'。
    一个字符串的反码定义为其所有字符的反码。我们的任务就是计算出给定字符串的反码。

输入:

    输入每行都是一个字符串,字符串长度不超过 80 个字符。如果输入只有!,表示输入结束,不需要处理。

输出:

对于输入的每个字符串,输出其反码,每个数据占一行。

样例输入:
Hello 
JLU-CCST-2011 
!
样例输出:
Svool 
QOF-XXHG-2011

这道题得用gets()读入,不然Presentation Error
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #define MAX 102
 7  
 8 char toDeal[MAX];
 9 int main(int argc, char const *argv[])
10 {
11     int n, m, k;
12     //freopen("input.txt","r",stdin);
13     gets(toDeal);
14     while(strcmp(toDeal, "!") != 0) {
15        for(int i = 0; i < strlen(toDeal); i++) {
16           if(toDeal[i] >= 'a' && toDeal[i] <= 'z') {
17               printf("%c", 'a' + 'z' - toDeal[i]);
18           }
19           else if(toDeal[i] >= 'A' && toDeal[i] <= 'Z') {
20               printf("%c", 'A' + 'Z' - toDeal[i]);
21           }
22           else {
23               printf("%c", toDeal[i]);
24           }
25        }
26        puts("");
27        gets(toDeal);
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/jasonJie/p/5719909.html