7-1 字符串替换 (15 分)
本题要求编写程序,将给定字符串中的大写英文字母按以下对应规则替换:
原字母 | 对应字母 |
---|---|
A | Z |
B | Y |
C | X |
D | W |
… | … |
X | C |
Y | B |
Z | A |
输入格式:
输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
输出格式:
输出在一行中给出替换完成后的字符串。
输入样例:
Only the 11 CAPItaL LeTtERS are replaced.
输出样例:
Lnly the 11 XZKRtaO OeGtVIH are replaced.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
char a[]= {'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'};
char b[]= {'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};
for(int i=0;i<x.length();i++) {
char x1=x.charAt(i);
boolean f=false;
int y=0;
for(int j=0;j<26;j++) {
if(x1==a[j]) {
f=true;
y=j;
}
}
if(f==true) {
System.out.print(b[y]+"");
}
else {
System.out.print(x1);
}
}
}
}