习题7-7 字符串替换 (15 分)
本题要求编写程序,将给定字符串中的大写英文字母按以下对应规则替换:
输入格式:
输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
输出格式:
输出在一行中给出替换完成后的字符串。
输入样例:
Only the 11 CAPItaL LeTtERS are replaced.
输出样例:
Lnly the 11 XZKRtaO OeGtVIH are replaced.
提交:
#include <stdio.h> #include <string.h> int main() { int i,n,j; char a[81]; gets(a); n = strlen(a); // char az[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; // for (i=0; i<n; i++) { // for (j=0; j<26; j++) { // if (a[i] == az[j]){ // a[i] = az[25-j]; // break; // } // } // printf("%c",a[i]); // } for (i=0; i<n; i++) { if (a[i] >= 'A' && a[i] <= 'Z') a[i] = 'A' + 'Z' - a[i]; printf("%c",a[i]); } return 0; }