0
package javaapplication40;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
*
* @author OEM
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter number of sentence");
x=Integer.parseInt(br.readLine());
if(x>10)
System.out.println("out of range");
else
{
String s[]=new String[x];
for(i=0;i<x;i++)
{
System.out.println("Enter "+(i+1)+" string");
s[i]=br.readLine();
}
for(i=0;i<x;i++)
{
if(i%2==0)
sort(s[i]);
else
rev(s[i]);
}
}
}catch(Exception e){}
}
static void sort(String s)
{
int x,i=0,j;
String temp;
StringTokenizer st=new StringTokenizer(s);
x=st.countTokens();
String a[]=new String[x];
while(st.hasMoreTokens())
{
a[i++]=st.nextToken();
}
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i].compareTo(a[j])>0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<x;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void rev(String s)
{
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
String s1;
s1=st.nextToken();
StringBuffer str=new StringBuffer(s1);
System.out.print(str.reverse().toString()+" ");
}
System.out.println();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
*
* @author OEM
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter number of sentence");
x=Integer.parseInt(br.readLine());
if(x>10)
System.out.println("out of range");
else
{
String s[]=new String[x];
for(i=0;i<x;i++)
{
System.out.println("Enter "+(i+1)+" string");
s[i]=br.readLine();
}
for(i=0;i<x;i++)
{
if(i%2==0)
sort(s[i]);
else
rev(s[i]);
}
}
}catch(Exception e){}
}
static void sort(String s)
{
int x,i=0,j;
String temp;
StringTokenizer st=new StringTokenizer(s);
x=st.countTokens();
String a[]=new String[x];
while(st.hasMoreTokens())
{
a[i++]=st.nextToken();
}
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(a[i].compareTo(a[j])>0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<x;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void rev(String s)
{
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
String s1;
s1=st.nextToken();
StringBuffer str=new StringBuffer(s1);
System.out.print(str.reverse().toString()+" ");
}
System.out.println();
}
}
adnAn93
Feb 10, '13
1 answers
1
We are sorting the sentences stored at the even indeces of the string array in alphabetical order and reversing each word of the sentences at the odd indeces.
Give me some time. I'll do it in a simpler way.
khanmansoor
Feb 10, '13
Log in to your account to answer this questions.