import java.util.Scanner;
public class JPA410 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s, c1, c2;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c1 = keyboard.nextLine();
System.out.print("Input another character: ");
c2 = keyboard.nextLine();
System.out.printf("%s\n", replace(s, c1, c2));
}
static String replace(String s,String c1,String c2){
if(s.equals("")){
return "";
}
else if (s.substring(0,1).equals(c1)){
return c2+replace(s.substring(1),c1,c2);
}
else{
return s.substring(0,1)+replace(s.substring(1),c1,c2);
}
}
}
410 遞迴字串替換
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言