File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments