題目1105:字串的反碼

來源:互聯網
上載者:User

標籤:

題目描述:

    一個位元,將其每一位取反,稱之為這個數的反碼。下面我們定義一個字元的反碼。如果這是一個小寫字元,則它和字元‘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 }

 

題目1105:字串的反碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.