forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCharacter.java
More file actions
49 lines (42 loc) · 1.32 KB
/
Copy pathRemoveCharacter.java
File metadata and controls
49 lines (42 loc) · 1.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package strings.Assignment;
import java.util.Scanner;
/*For a given a string(str) and a character X, write a function to remove all the occurrences of X from the given string.
The input string will remain unchanged if the given character(X) doesn't exist in the input string.
Input Format:
The first line of input contains a string without any leading and trailing spaces.
The second line of input contains a character(X) without any leading and trailing spaces.
Output Format:
The only line of output prints the updated string.
Note:
You are not required to print anything explicitly. It has already been taken care of.
Constraints:
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
Sample Input 1:
aabccbaa
a
Sample Output 1:
bccb
Sample Input 2:
xxyyzxx
y
Sample Output 2:
xxzxx*/
public class RemoveCharacter {
public static String removeCharacter(String str, char ch) {
String answer = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ch) {
answer += str.charAt(i);
}
}
return answer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str1 = scan.next();
char ch = scan.next().charAt(0);
System.out.println(removeCharacter(str1, ch));
}
}