forked from task032015/stringProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringProblem.java
More file actions
40 lines (26 loc) · 1.03 KB
/
Copy pathStringProblem.java
File metadata and controls
40 lines (26 loc) · 1.03 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
import java.io.FileNotFoundException;
import java.io.IOException;
public class StringProblem {
public static void main(String args[]) throws FileNotFoundException, IOException {
String str = "Thes code for the sring problem";
System.out.println("Method 1: " + replaceString1(str));
System.out.println("Method 2: " + replaceString2(str));
}
public static String replaceString1(String str) {
char[] strChars = str.toCharArray();
for (int i = strChars.length - 1; i >= 0; i--) {
if (strChars[i] == 's')
str = str.replace("s","");
}
return str;
}
public static String replaceString2(String str) {
char[] strChars = str.toCharArray();
for (int i = 0; i <= strChars.length-1; i++) {
if (strChars[i] == 's')
strChars[i] = ' ';
}
String output = new String(strChars);
return output;
}
}