Sunday, January 12, 2014

BlueJ Program On Word Replacement In A Sentence



In this BlueJ program, user will enter a sentence, a word to search and another word to replace. The searched word will be replaced by the specified word and the operation will be carried out irrespective of letter case.


import java.io.*;
class Str1
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 String s1,s2,s3,s4="",str[]=new String[20];
 int i,x=0;
  public void takeText() throws Exception
 {
  System.out.println("Enter the sentence:");
  s1=br.readLine();
  System.out.println("Enter the word to replace:");
  s2=br.readLine();
  System.out.println("With which word replacement will be done?");
  s3=br.readLine();
  System.out.println("Original sentence="+s1);
  while(true)
  {
   i=s1.indexOf(' ');
   if(i< 0)
   break;
   str[x++]=s1.substring(0,i);
   s1=s1.substring(i+1);
   }
   str[x++]=s1;
 for(i=0;i< x;i++)
 {
  if(s2.equalsIgnoreCase(str[i]))
  s4=s4+" "+s3;
  else
  s4=s4+" "+str[i];
  }
  System.out.println("Modified sentence="+s4);
    }
    public static void main(String args[]) throws Exception
    {
Str1 ob=new Str1();
    ob.takeText();
    }
    }

Sample input and output of the program


Sample Input: Disari Public College is located in Haldia. It is co-ed residential cum day boarding College.
Search Keyword: College
Replace Keyword: School
Sample Output : Disari Public School is located in Haldia. It is co-ed residential cum day boarding School. 

2 comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner