Skip to content

Instantly share code, notes, and snippets.

@peter279k peter279k/
Created May 24, 2017

What would you like to do?
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(;
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
You can’t perform that action at this time.