-
Notifications
You must be signed in to change notification settings - Fork 16
/
19.c
45 lines (38 loc) · 1.04 KB
/
19.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
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 100
#define MAX_LENGTH 100
int main()
{
char strings[MAX_STRINGS][MAX_LENGTH];
char temp[MAX_LENGTH];
int numStrings, i, j;
printf("Enter the number of strings: ");
scanf("%d", &numStrings);
getchar();
printf("Enter the strings:\n");
for (i = 0; i < numStrings; i++)
{
fgets(strings[i], sizeof(strings[i]), stdin);
strings[i][strcspn(strings[i], "\n")] = '\0';
}
// Sort the strings in lexicographical order
for (i = 0; i < numStrings - 1; i++)
{
for (j = i + 1; j < numStrings; j++)
{
if (strcmp(strings[i], strings[j]) > 0)
{
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]);
strcpy(strings[j], temp);
}
}
}
printf("\nStrings in lexicographical order:\n");
for (i = 0; i < numStrings; i++)
{
printf("%s\n", strings[i]);
}
return 0;
}