Skip to content

Commit 19660e8

Browse files
Merge pull request #257 from nipun-das/Nipun-Das-patch-1
added a beautiful code for lexicographic sorting
2 parents 1caf628 + c480652 commit 19660e8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Lexicographic_Sorting.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Lexicographic sorting is the way of sorting words based on the alphabetical order of their component letters.
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
void main()
6+
{
7+
char str[20][20], temp[20];
8+
int n, i, j;
9+
printf("Enter the Number of Strings:\n");
10+
scanf("%d", &n);
11+
12+
// Getting strings input
13+
printf("Enter the Strings:\n");
14+
for (i = 0; i < n; i++)
15+
{
16+
scanf("%s", str[i]);
17+
}
18+
19+
// storing strings in the lexicographical order
20+
for (i = 0; i < n - 1; i++)
21+
{
22+
for (j = 0; j < n - 1 - i; j++)
23+
{
24+
if (strcmp(str[j], str[j + 1]) > 0)
25+
{
26+
// swapping strings if they are not in the lexicographical order
27+
strcpy(temp, str[j]);
28+
strcpy(str[j], str[j + 1]);
29+
strcpy(str[j + 1], temp);
30+
}
31+
}
32+
}
33+
printf("Strings in the Lexicographical Order is:\n");
34+
for (i = 0; i < n; i++)
35+
{
36+
puts(str[i]);
37+
}
38+
}

0 commit comments

Comments
 (0)