Description
To sort set of strings in alphabetical order, user will take set of strings and program will sort the set of strings in alphabetical order.
C/C++
/* C Program to Sort set of strings in alphabetical order*/
//Save it as SortSetOfStrings.c
#include<stdio.h>
#include<string.h>
int main(){
int i,j,n;
char str[50][50],temp[50];
printf("Enter number of strings : ");
scanf("%d",&n);
printf("Enter the strings : ");
for(i=0;i<=n;i++){
gets(str[i]);
}
for(i=0;i<=n;i++){
for(j=(i+1);j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
printf("The strings after sorting : ");
for(i=0;i<=n;i++){
puts(str[i]);
}
return 0;
}
Input: Enter number of strings : 5 Enter the strings : Raman Ajeet Vicky Krishna Meera Output: The strings after sorting : Ajeet Krishna Meera Raman Vicky
Java
/* Java Program to Sort set of strings in alphabetical order*/
//Save it as SortSetOfStrings.java
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class SortSetOfStrings {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i, n;
System.out.println("Enter number of strings : ");
n = scanner.nextInt();
String str[] = new String[n+1];
System.out.println("Enter the strings : ");
for(i=0;i<=n;i++) {
str[i] = scanner.nextLine();
}
Arrays.sort(str);
System.out.println("The strings after sorting : ");
for(i=0;i<=n;i++) {
System.out.println(str[i]);
}
}
}
Input: Enter number of strings : 5 Enter the strings : Raman Ajeet Vicky Krishna Meera Output: The strings after sorting : Ajeet Krishna Meera Raman Vicky
Related Programs
1) Program to sort characters of strings in alphabetical order2) Program to Convert Lowercase to Uppercase of a String
3) Program to Append one String to another String
4) Program to Compare Two Strings
5) Program to Reverse a String
6) Find Substring of a Given String
7) Program to Insert a String into Another String
8) Program to check String is Palindrome
9) Program to check entered character is vowel or consonants
10) Program to remove vowels from a String
11) Program to count uppercase and lowercase alphabet characters in String