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