Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class testHello{
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter a string: ");
- String string = sc.nextLine();
- System.out.print("Enter a character to be replaced: ");
- char replace = sc.next().charAt(0);
- System.out.print("Enter the replacement character: ");
- char replacement = sc.next().charAt(0);
- String output = "";
- for(int i = 0; i < string.length(); i++)
- if(string.charAt(i) == replace) output += replacement;
- else output += string.charAt(i);
- System.out.print("The new string is: "+output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement