Write a Java program to find a certain character in a string and replace it with a new character.
Allow user to enter a string, find character and the replace character.
Display the new string.
import java.util.Scanner;
public class Q161645 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string, findChar, replaceChar, newString;
System.out.print("Enter String:");
string = scanner.next(); // Get user input for initial string
System.out.print("Enter find character: ");
findChar = scanner.next(); // Get user input for find character
System.out.print("Enter replace character: ");
replaceChar = scanner.next(); // Get user input for replace character
/* Replace each find character in initial string with replace character */
newString = string.replace(findChar, replaceChar);
System.out.println("The new string is " + newString);
}
}
Comments
Leave a comment