Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created May 24, 2017 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/b27186ccf9125c155d3edc84a10a2eea to your computer and use it in GitHub Desktop.
Save peter279k/b27186ccf9125c155d3edc84a10a2eea to your computer and use it in GitHub Desktop.
TQC+ JAVA
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