Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code for lexicographic sort #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions C/lexicographic_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]);
}
}