0% found this document useful (0 votes)
22 views1 page

Sort String

The document contains code for a Java program that takes user input of multiple strings, sorts them alphabetically, and outputs the sorted list. The program uses arrays and loops to store the input strings, compare them, and sort them into alphabetical order.

Uploaded by

Souvik Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

Sort String

The document contains code for a Java program that takes user input of multiple strings, sorts them alphabetically, and outputs the sorted list. The program uses arrays and loops to store the input strings, compare them, and sort them into alphabetical order.

Uploaded by

Souvik Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class SortString
{
public static void main(String[] args)
{
int count;
String temp;
Scanner scan = new Scanner(System.in);

System.out.println("Enter number of strings:");


count = scan.nextInt();

String str[] = new String[count];


Scanner scan2 = new Scanner(System.in);

System.out.println("Enter the Strings one by one:");


System.out.println("");
for(int i = 0; i < count; i++)
{
str[i] = scan2.nextLine();
}
scan.close();
scan2.close();

for (int i = 0; i < count; i++)


{
for (int j = i + 1; j < count; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

System.out.println("Strings in Sorted Order:");


System.out.println("");
for (int i = 0; i <= count - 1; i++)
{
System.out.println(str[i]);
}
}
}

You might also like