Sie sind auf Seite 1von 5

Question:

Write down a java program to accept ‘N’ words and print all the words as per user’s choice:
1. Original order of input
2. Reverse order of input
3. Ascending order of input
4. Descending order of input
Use function Order() to arrange the data. Use function Display() to display the data. Use function InPuT() to take input.

Algorithm:
Step 1: Start
Step 2:

Program:

import java.io.*;
public class word
{
void inPut(String X[]) throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader (read);
int LN=X.length;
for(int i=0; i<LN;i++)
{
System.out.println("Enter a word");
X[i]=in.readLine();
X[i]=X[i].trim() + " ";
X[i]=(X[i].substring(0,(X[i].indexOf(' '))));//extract the first word
}
}
void Order(String X[], int ch)// function
{
int LN=X.length; //length of word
String temp=" "; //temporary String for swapping
switch(ch)
{
case 1: //empty case as the input and output are in same order
break;
case 2:
for (int i=0; i<LN/2;i++)
{
temp = X[i];
X[i]=X[LN-1-i];
X[LN-1-i]=temp;
}
break;
case 3:
int j;
for( int i=0;i<(LN-1);i++)
{
for (j=0;j<(LN-1-i);j++)
{
if(X[j].compareTo(X[j+1])>0)//arrange data in ascending order of alphabet
{
temp=X[j];
X[j]=X[j+1];
X[j+1]=temp;
}
}
}
break;
case 4:
for(int i=0;i<(LN-1);i++)
{
for(j=0; j<(LN-1-i);j++)
{
if(X[j].compareTo(X[j+1])<0) //arrange data in descending order of alphabet
{
temp = X[j];
X[j]=X[j+1];
X[j+1]=temp;
}
}
}
break;
default:
System.out.println("INVALID INPUT!!");
break;
}
}
void Display(String X[])
{
int LN=X.length;
for(int i=0;i<LN;i++)
{
System.out.print(X[i]+ " "); //to display
}
}
public static void main() throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader (read);
System.out.println("Enter the number of words to store in array");
int N=Integer.parseInt(in.readLine());
String a[]=new String[N];
System.out.println("Choose the following");
System.out.println(" 1. Display words in original order as entered");
System.out.println(" 2. Display words in reverse order of input");
System.out.println(" 3. Display words in ascending order of input");
System.out.println(" 4. Display words in descending order of input");
int chc= Integer.parseInt(in.readLine());
word ob=new word();
ob.inPut(a);
ob.Order(a,chc);
ob.Display(a);
}
}

Variable Description:

Data Type Variable Purpose


X[] To store the array elements
LN To check length of word
X Store the word entered by user
temp To temporary variable for swapping
i ‘For’ loop Variable
ch Switch case variable
j Used for inner loop variable
a[] Used in main method function to pass
the value to function
N Used in main method To pass the
value to function
chc

Output:
1) Enter the number of words to store in array
6
Choose the following
1. Display words in original order as entered
2. Display words in reverse order of input
3. Display words in ascending order of input
4. Display words in descending order of input
3
Enter a word
Happy
Enter a word
Sad
Enter a word
Angry
Enter a word
Crying out loud
Enter a word
Funny
Enter a word
Cranky
Angry Cranky Crying Funny Happy Sad

2) Enter the number of words to store in array


5
Choose the following
1. Display words in original order as entered
2. Display words in reverse order of input
3. Display words in ascending order of input
4. Display words in descending order of input
4
Enter a word
Havana
Enter a word
Starboy
Enter a word
Closer
Enter a word
Colors
Enter a word
Eastside
Starboy Havana Eastside Colors Closer

3) Enter the number of words to store in array


8
Choose the following
1. Display words in original order as entered
2. Display words in reverse order of input
3. Display words in ascending order of input
4. Display words in descending order of input
2
Enter a word
Shakira
Enter a word
Halsey
Enter a word
Camila
Enter a word
Benny Blanco
Enter a word
Justin
Enter a word
Khalid
Enter a word
Nikki
Enter a word
Ariana
Ariana Nikki Khalid Justin Benny Camila Halsey Shakira

4) Enter the number of words to store in array


4
Choose the following
1. Display words in original order as entered
2. Display words in reverse order of input
3. Display words in ascending order of input
4. Display words in descending order of input
1
Enter a word
Bonjour!
Enter a word
Hola!
Enter a word
Namaste!
Enter a word
Greetings!
Bonjour! Hola! Namaste! Greetings!

Das könnte Ihnen auch gefallen