-
Notifications
You must be signed in to change notification settings - Fork 8
/
Lexicographic_Sorting.c
38 lines (35 loc) · 978 Bytes
/
Lexicographic_Sorting.c
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
// Lexicographic sorting is the way of sorting words based on the alphabetical order of their component letters.
#include <stdio.h>
#include <string.h>
void main()
{
char str[20][20], temp[20];
int n, i, j;
printf("Enter the Number of Strings:\n");
scanf("%d", &n);
// Getting strings input
printf("Enter the Strings:\n");
for (i = 0; i < n; i++)
{
scanf("%s", str[i]);
}
// storing strings in the lexicographical order
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (strcmp(str[j], str[j + 1]) > 0)
{
// swapping strings if they are not in the lexicographical order
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
printf("Strings in the Lexicographical Order is:\n");
for (i = 0; i < n; i++)
{
puts(str[i]);
}
}