Skip to content

Commit 3688cc0

Browse files
committed
[Manan] ADD:Removing a specific character from a string
1 parent 03ee027 commit 3688cc0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

RemoveCharacter.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
3+
class RemoveCharacter {
4+
// Main method
5+
public static void main(String[] args) {
6+
Scanner input = new Scanner(System.in);
7+
8+
System.out.print("Enter a string: ");
9+
String str = input.nextLine();
10+
11+
System.out.print("Enter a character to remove: ");
12+
char ch = input.next().charAt(0);
13+
14+
// Method to remove a specific character from the string
15+
String result = removeCharacter(str, ch);
16+
17+
System.out.println("String after removing character: " + result);
18+
19+
input.close();
20+
}
21+
22+
// Method to remove a specific character from the string
23+
public static String removeCharacter(String str, char ch) {
24+
String result = "";
25+
for (int i = 0; i < str.length(); i++) {
26+
if (str.charAt(i) != ch) {
27+
result += str.charAt(i);
28+
}
29+
}
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)