-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteWordInString.java
More file actions
34 lines (28 loc) · 893 Bytes
/
DeleteWordInString.java
File metadata and controls
34 lines (28 loc) · 893 Bytes
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
package algorithms;
import java.util.Scanner;
/**
*
* @author Gökhan DAĞTEKİN
*
* 2015
*/
public class DeleteWordInString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String string = sc.nextLine();
System.out.println("Enter a word in string: ");
String word = sc.nextLine();
String deletedSentences = deleteWord(string, word);
System.out.println(deletedSentences);
}
private static String deleteWord(String sentence, String word) {
int index = sentence.indexOf(word);
int stringLength = word.length();
while (index >= 0) {
sentence = sentence.substring(0, index) + sentence.substring((index + 1 + stringLength));
index = sentence.indexOf(word);
}
return sentence;
}
}