-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremove_word.java
More file actions
74 lines (67 loc) · 2 KB
/
remove_word.java
File metadata and controls
74 lines (67 loc) · 2 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Program to Remove given Word from a String
import java.util.*;
class remove_word
{ public static StringBuffer remove(StringBuffer str,StringBuffer str2)
{
int l1=str.length();
int l2=str2.length();
int high=0,low=0;
int j=0;
for(int i=0;i<l1;i++)
{
if(j>=l2)
{
break;
}
if(str.charAt(i)==str2.charAt(j))
{
j++;
if(j==l2)
{
high=i;
high++;
}
}
else
{
j=0;
}
}
low=high-l2;
str.delete(low,high);
// System.out.println("String after del is"+str);
return str;
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string :");
StringBuffer str=new StringBuffer(sc.nextLine());
System.out.println("Enter the word you want to remove :");
StringBuffer str2=new StringBuffer(sc.nextLine());
StringBuffer str3=new StringBuffer();
str3=remove(str,str2);
System.out.println("The String after removing the word is :"+str3);
}
}
//Geeks for Geeks method
/*
public class GFG {
public static String removeWord(String string, String word)
{
if (string.contains(word))
{
String tempWord = word + " ";
string = string.replaceAll(tempWord, "");
tempWord = " " + word;
string = string.replaceAll(tempWord, "");
}
return string;
}
public static void main(String args[])
{
String string1 = "Geeks for Geeks.";
String word1 = "for";
System.out.println("String: " + string1 + "\nWord: " + word1+ "\nResult String: "+ removeWord(string1, word1));
}
} */