-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplace.java
More file actions
29 lines (29 loc) · 994 Bytes
/
Copy pathReplace.java
File metadata and controls
29 lines (29 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
public class Replace{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String string = sc.nextLine();
string = string.toLowerCase();
char[] arr = string.toCharArray();
System.out.println("Enter first char: ");
char ch1 = sc.next().charAt(0);
System.out.println("Enter second char: ");
char ch2 = sc.next().charAt(0);
Replace replace = new Replace();
String res = replace.replaceChar(arr, ch1, ch2);
System.out.println("Result: " + res);
sc.close();
}
private String replaceChar(char[] arr, char ch1, char ch2){
for(int i = 0; i < arr.length; i++){
if(arr[i] == ch1){
arr[i] = ch2;
}
else if(arr[i] == ch2){
arr[i] = ch1;
}
}
return new String(arr);
}
}