Tuesday, October 20, 2020

BlueJ Program To Remove Any Word From A Sentence

 In this BlueJ Program, take any sentence and a particular word. Remove the word(s) from the sentence (if present).

import java.util.*;
class Frequency
{
  int i;
  String str,s,word,str1="";
Scanner sc=new Scanner(System.in);
public void take()
{
     System.out.print("\nEnter the sentence:");
     str=sc.nextLine();
     str=str+" ";
     System.out.print("\nEnter the word to be removed:");
     word=sc.nextLine();
      while(true)
     {
          i=str.indexOf(' ');
          if(i<0)
          break;
          s=str.substring(0,i);
          str=str.substring(i+1);
          if(!s.equals(word))
          str1=str1+ " " + s;
      }
      str1=str1.trim();
      System.out.print("\nModified Sentence is: "+str1);
    }
    public static void main(String args[])
    {
         Frequency ob=new Frequency();
         ob.take();
    }

}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner