时间限制:C/C++语言 2000MS;其他语言 4000MS
内存限制:C/C++语言 10240KB;其他语言 534528KB
题目描述:
一个字符串,含有字母数字和特殊符号等,按照以下要求写一个转换函数
1)只保留小写字母,过滤掉其他字符,
2)对过滤结果按照下述规则进行映射转换:
例如若right = 2 :
转换前->转换后
a->c
b->d
c->e
…
x->z
y->a
z->b
例如1 输入字符串:”difg123” right =3, 则最后输出为” glij”
例如2 输入字符串:”yz” 右偏移为3, 则最后输出为” bc”
输入
输入占两行,包括一个字符串,一个偏移值r
输出
字符串
样例输入
difg123
3
样例输出
glij
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String s="";
int r=0;
while(cin.hasNext())
{
s = cin.next();
r = cin.nextInt();
System.out.println(convert(s, r));
}
}
public static String convert(String str, int right){
String res="";
for(int i = 0; i < str.length(); i ++)
{
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
{
if((str.charAt(i) + right) > 'z')
{
//str.charAt(i) - 26:向前移动26个字母
res += (char)(str.charAt(i) - 26 + right);
}else {
res += (char)(str.charAt(i) + right);
}
}
}
return res;
}
}