IQ

Ad SpeedyAds

Friday, March 9, 2012

Java program to print the words of a sentence in reverse

import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int L=S.length(),a=L;
for(int i=L-1; i>=0; i--)
{
if(S.charAt(i)==32)
{
System.out.print(S.substring(i+1,b)+" ");
b=i;
}
}
}
}

Thursday, March 8, 2012

Javaprogram to print the first and last letters of all words in a program

import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int a=1, L=S.length();
for(int i=1; i {
if(S.charAt(i)==32)
{
System.out.print(S.charAt(a));
if((i-1)!=a)
System.out.print(S.charAt(i-1));
System.out.print(" ");
a=++i;
}
}
}
}