Update OOP/Java/Lab/Week9/replacing.java
This commit is contained in:
parent
6480627716
commit
c910a492db
@ -1,27 +1,26 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
class replacing{
|
class replacing{
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner in=new Scanner(System.in);
|
Scanner in=new Scanner(System.in);
|
||||||
System.out.println("Enter the Lines...");
|
System.out.println("Enter the Lines...");
|
||||||
String str=in.nextLine();
|
String str=in.nextLine();
|
||||||
str.trim();
|
str.trim();
|
||||||
System.out.println("Enter the word to be removed");
|
System.out.println("Enter the word to be removed");
|
||||||
String word=in.nextLine();
|
String word=in.nextLine();
|
||||||
StringBuffer strbuf= new StringBuffer(str);
|
StringBuffer strbuf= new StringBuffer(str);
|
||||||
if(str.contains(word))
|
if(str.contains(word))
|
||||||
{
|
{
|
||||||
|
|
||||||
int index=str.indexOf(word);
|
int index=str.indexOf(word);
|
||||||
int wlen=word.length();
|
int wlen=word.length();
|
||||||
String
|
String rep="";
|
||||||
String rep="";
|
for(int i=0;i<wlen;i++)
|
||||||
for(int i=0;i<wlen;i++)
|
{
|
||||||
{
|
rep+="*";
|
||||||
rep+="*";
|
}
|
||||||
}
|
strbuf.replace(index,index+wlen,rep);
|
||||||
strbuf.replace(index,index+wlen,rep);
|
}
|
||||||
}
|
System.out.println("Replaced String=\n"+strbuf);
|
||||||
System.out.println("Replaced String=\n"+strbuf);
|
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user