KEMBAR78
Prog2 Substring Removal | PDF
0% found this document useful (0 votes)
6 views3 pages

Prog2 Substring Removal

The document presents a Java program that removes a specified substring from a main string using the StringBuffer class. It includes a method to read user input for both the main string and the substring, and another method to perform the removal operation. The program outputs the modified string or indicates if the substring does not exist in the main string.

Uploaded by

Sam Sun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Prog2 Substring Removal

The document presents a Java program that removes a specified substring from a main string using the StringBuffer class. It includes a method to read user input for both the main string and the substring, and another method to perform the removal operation. The program outputs the modified string or indicates if the substring does not exist in the main string.

Uploaded by

Sam Sun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

SUBSTRING REMOVAL USING STRINGBUFFER CLASS

import java.io.*;

class Removal
{
String str,sub;
int i=0,flag=0;
void getString() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter the main String:");
str=br.readLine();
System.out.println("Enter the Substring:");
sub=br.readLine();
}

void removeString()
{
StringBuffer strbuf=new StringBuffer(str);
int l1,l2,l=0;
l1=strbuf.length();
l2=sub.length();
if((l2>l1)||(l2<0))
System.out.print("Invalid String");
else
{
do
{
String s=strbuf.substring(i,i+l2);
if(s.equals(sub))
{
strbuf.delete(i,i+l2);
flag=1;
break;
}
i++;
l=strbuf.length();
}while((l-i)>=l2);
if(flag==0)
System.out.println("No Such Substring Exist");
else
System.out.println("New String is:"+strbuf);
}
}
}
class Ex2
{
public static void main(String args[]) throws IOException
{
Removal r=new Removal();
r.getString();
r.removeString();
}
}
OUTPUT

D:\AMJPracticals\JAVA>javac Ex2.java

D:\AMJPracticals\JAVA>java Ex2
Enter the main String:
AM JAIN COLLEGE
Enter the Substring:
AM
New String is: JAIN COLLEGE

You might also like