-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSortingOfStrings.java
44 lines (41 loc) · 1.05 KB
/
SortingOfStrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class SortingOfStrings{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String tmp;
int i,j,n;
System.out.println("Enter the number of names you want to enter : ");
n = scan.nextInt();
String[] arr = new String[n];
for(i=0;i<n;i++)
{
try
{
System.out.println("Enter the string "+(i+1)+" : ");
arr[i] = scan.next();
}
catch(Exception e)
{
System.out.println(e);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j].compareTo(arr[j+1]) > 0)
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
System.out.println("After sorting : ");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
}