forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexicographic.c
47 lines (47 loc) · 1.17 KB
/
Lexicographic.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
46
47
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, r, c;
char str[5][60];
//-----------taking words from user----------
printf("Enter 5 words\n");
for (r = 0; r < 5; r++)
{
scanf("%s", str[r]);
}
printf("\n");
char temp[50]; //for swapping in correct order it is used
// below is code for sorting elements in order
for (i = 0; i < 5; i++)
{
for (j = i + 1; j <= 4; j++)
{
if (strcmp(str[i], str[j]) > 0) //If ascii value of str 1 is greater than swap
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
// for (i = 0; i < words; i++)
// {
// for (j = i + 1; j <words; j++)
// {
// if (strcmp(str[i], str[j]) > 0)
// {
// strcpy(temp, str[i]);
// strcpy(str[i], str[j]);
// strcpy(str[j], temp);
// }
// }
// }
//printing sorted elements
printf("In lexicographical order: \n");
for (i = 0; i < 5; ++i)
{
printf(str[i]);
printf("\n");
}
}