Created
May 24, 2017 17:12
-
-
Save peter279k/b27186ccf9125c155d3edc84a10a2eea to your computer and use it in GitHub Desktop.
TQC+ JAVA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class JPA04 { | |
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\r\n", replace(s, c1, c2)); | |
} | |
public static String replace(String s, String c1, String c2) { | |
if(s.equals("")) { | |
return s; | |
} | |
if(s.substring(0, 1).equals(c1)) { | |
return c2 + replace(s.substring(1), c1, c2); | |
} | |
return s.substring(0, 1) + replace(s.substring(1), c1, c2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment