-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceChar.java
More file actions
33 lines (24 loc) · 834 Bytes
/
ReplaceChar.java
File metadata and controls
33 lines (24 loc) · 834 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
30
31
import java.util.*;
public class ReplaceChar {
public static void main(String args[])
{
String strNew = new String();
String str = new String();
char ch,chNew;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
str = sc.nextLine();
System.out.print("Enter the element to replace: ");
ch = sc.next().charAt(0);
System.out.print("Enter the charcter to be replaced: ");
chNew = sc.next().charAt(0);
for(int i=0;i<str.length();i++)
{
if(ch == str.charAt(i))
{
strNew = str.replace(str.charAt(i), chNew);
}
}
System.out.print("The new string is: "+strNew);
}
}