forked from VimalKrishnaRao/OOP-in-Java-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceCharacter.java
More file actions
31 lines (30 loc) · 774 Bytes
/
Copy pathReplaceCharacter.java
File metadata and controls
31 lines (30 loc) · 774 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.Scanner;
class ReplaceCharacter
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String str = sc.nextLine();
System.out.println("Enter a character to replace:");
char c1 = sc.next().charAt(0);
System.out.println("Enter the replacement character:");
char c2 = sc.next().charAt(0);
str = str.toLowerCase();
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
String result = "";
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == c1)
{
result += c2;
}
else
{
result += str.charAt(i);
}
}
System.out.println("Modified string: " + result);
}
}