Ques:- Design a class Exchange to accept a sentence and interchange the first and last alphabet with each other.Do same for all the words present in a string,but single letter word will be unchanged.
For example:- INPUT | It is a warm day
OUTPUT| tI si a marw yad
/* Design a class Exchange to accept a sentence and interchange the first and last alphabet
with each other.Do same for all the words present in a string,but single letter word will be unchanged.
For example:- INPUT | It is a warm day
OUTPUT| tI si a marw yad*/
import java .io.*;
class Exchange
{
String sent,rev;
int size;
Exchange()
{
sent=rev="";
size=0;
}
void read()throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter a string");
sent=br.readLine();
size=sent.length();
}
void ex_first_last()
{
char ch,ch1,ch2,ch3;
String wd="",mid;
int i;
for(i=0;i<size;i++)
{
ch=sent.charAt(i);
if(ch!=' ')
{
wd+=ch;
}
else
{
int len1=wd.length();
ch1=wd.charAt(0);
ch2=wd.charAt(len1-1);
ch3=ch1;
ch1=ch2;
ch2=ch3;
mid=wd.substring(1,len1);
rev=ch1+mid+ch2+" ";
wd="";
}
}
}
void display()
{
System.out.println("New String "+rev);
}
}