Saturday, December 10, 2016

BlueJ Program On Exchange Of Position Between Consecutive Letters In A Sentence



In this BlueJ program, the consecutive letters in a sentence will exchange their positions. If the sentence has odd number of characters then the last character will remain in it's position

Example: Enter the Sentence: This is a test of twist
Original Sentence: This is a test of twist
Modified Sentence: hTsii  s aettso  fwtsit
(The above sentence has 23 characters)


Example: Enter the Sentence:Oh Mahua
Original Sentence: Oh Mahua
Modified Sentence: hOM haau
(The above sentence has 08 characters)




 import java.io.*;
class TwistWord
{
String s,s1="";
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.print("\nEnter the Sentence:");
s=br.readLine();
}
public void show()
{
 System.out.println("Original Sentence:"+s);
 System.out.println("Modified Sentence:"+s1);
}
public void exchange()
{
 int flag=0;
len=s.length();
if(len%2!=0)
{
 len=len-1;
flag=1;
}

for(int i=0;i< len;i=i+2)
{
 s1=s1+s.charAt(i+1)+s.charAt(i);
}
if(flag==1)
s1=s1+s.charAt(len);
}
 public static void main(String args[]) throws Exception
{
 TwistWord ob=new TwistWord();
ob.take();
ob.exchange();
ob.show();
 }
}

Related Post: BlueJ Programs on String/Sentence

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner