Skip to content

Commit d987cf1

Browse files
authored
Create 8 : Remove character
1 parent 7a73057 commit d987cf1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Strings/8 : Remove character

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
"Remove character"
3+
Given a string and a character x. Write a function to remove all occurrences of x character from the given string.
4+
Leave the string as it is, if the given character is not present in the string.
5+
6+
7+
Input Format :
8+
Line 1 : String S
9+
Line 2 : Character c
10+
Output Format :
11+
Modified string
12+
Constraints :
13+
0 <= |S| <= 10^7
14+
where |S| represents the length of string, S.
15+
Sample Input 1:
16+
welcome to coding ninjas
17+
o
18+
Sample Output 1:
19+
welcme t cding ninjas
20+
Sample Input 2:
21+
Think of edge cases before submitting solutions
22+
x
23+
Sample Output 2:
24+
Think of edge cases before submitting solutions
25+
26+
*/
27+
public class solution {
28+
29+
public static String removeAllOccurrencesOfChar(String input, char c) {
30+
String ans="";
31+
String C=Character.toString(c); //converting char c into String to use String.contains() method
32+
if(input.contains(C)==false){
33+
return input;
34+
}
35+
for(int i=0;i<input.length();i++){
36+
if(input.charAt(i)!=c){
37+
ans+=input.charAt(i);
38+
}
39+
}
40+
41+
return ans;
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)