Sunday, January 12, 2014

BlueJ Program To Sort The Words Of A Sentence In Ascending Order



In this BlueJ program, user will enter ant sentence and the words of the sentence will be arranged in ascending order irrespective of case.                 

import java.io.*;

class Str
{
String s1,s2;
String str[]=new String[10];
int i,j,x=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeString()throws Exception
{
System.out.println("Enter any sentence:");
s1=br.readLine();
System.out.println("Entered String="+s1);
while(true)
{
i=s1.indexOf(' ');
if(i< 0)
break;
str[x++]=s1.substring(0,i);
s1=s1.substring(i+1);
}
str[x++]=s1;

for(i=0;i< x;i++)
{
 for(j=i+1;j< x-1;j++)
{
 if(str[i].compareToIgnoreCase(str[j])>0)
{
s1=str[i];
str[i]=str[j];
str[j]=s1;
}
}
}
System.out.print("Modified String=");
for(i=0;i< x;i++)
{
 System.out.print(str[i]+ " ");
}
}
public static void main(String args[]) throws Exception
{
Str ob=new Str();
ob.takeString();
}
}

Sample input and output of the program


Sample Input :- India is My Countery
Output :- Country India is My

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner