Wednesday, February 23, 2011

Solution 4 - Words in reverse in a sentence

import java.io.*;
class vaibhav
{
public static void main(String args[])throws IOException
{
int i,j,c,x=0,u=0,k=0;String t;
BufferedReader o=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter a sentence==");
String s=o.readLine();
s=s+" ";
int l, l1;
l=s.length();
for(i=0;i {
char ch=s.charAt(i);
if(ch==' ')
{
t="";
t=s.substring(x,i);
l1=t.length();
char d=' ';
if(t.charAt(l1-1)=='.' || t.charAt(l1-1)=='?' || t.charAt(l1-1)=='!')
{
d=t.charAt(l1-1);
t=t.substring(0,l1-1);

}
StringBuffer f=new StringBuffer(t);
f.reverse();
System.out.print(f);
System.out.print(d+" ");
x=i+1;
}
}
}
}

No comments: