To sort
the given strings in alphabetical order
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[10][20], temp[20];
int n, i, j;
clrscr();
printf("Enter the number of strings : ");
scanf("%d", &n);
printf("\nEnter the strings : \n\n");
for(i = 0; i < n; i++)
scanf("%s", str[i]);
for(i = 0; i < n - 1; i++)
for(j = 0; j < n - 1; j++)
if(strcmp(str[j], str[j + 1]) > 0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
printf("\nThe sorted order of strings are : \n\n");
for(i = 0 ; i < n ; i++)
printf("%s \n", str[i]);
getch();
}
Output:
Enter the number of strings : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish
The sorted order of strings are :
bhuvan
priya
satish
udaya
viji
-
UpdatedDec 31, 2019
-
Views4,950
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order