Sunday, November 13, 2016

BlueJ Program To Remove First And Last Character Of Each Word From A Sentence





In this program, user will enter a sentence and first and last characters from each words of the sentence will be removed.

If any word in the sentence contains only 1 or 2 characters then the word will be removed.

Example: Input sentence : This is a test for removing characters from each words
         Output sentence: hi es o emovin haracter ro ac ord


import java.io.*;
import java.util.*;
class Pat
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk;
String s,s1,s2="";
int len;
public void takeSentence() throws Exception
{

System.out.print("Enter the sentence :");
s1=br.readLine();
}
public void modify()
{
stk=new StringTokenizer(s1);
while(stk.hasMoreTokens())
{
 s=stk.nextToken();
 len=s.length();
if(len >2)
 s2=s2+ " " + s.substring(1,len-1);

s2=s2.trim();
s1=s2;
}

public void show()
{
System.out.println(s1);
}
public static void main(String args[])throws Exception
{
 Pat ob=new Pat();
ob.takeSentence();
System.out.print("\nInput Sentence:");
ob.show();
ob.modify();
System.out.print("Output Sentence:");
ob.show();
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner