**
蓝桥杯练习-蓝桥杯-算法提高-9-2-文本加密
**
问题描述
先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:“A"转化"B”,“B"转化为"C”,… …“Z"转化为"a”,“a"转化为"b”,… …, “z"转化为"A”,其它字符不加密。编写程序,加密给定字符串。
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数的范围。
例:50个字符以内无空格字符串。
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
EncryptChar(s);
}
public static void EncryptChar(String str) {
String s = "";//定义一个新字符串进行串的拼接
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'Z') {
s += 'a';
} else if (str.charAt(i) == 'z') {
s += 'A';
} else {
s += (char) (str.charAt(i) + 1);
}
}
System.out.println(s);
}
}