lexicographical order in c

Solutions on MaxInterview for lexicographical order in c by the best coders in the world

showing results for - "lexicographical order in c"
Dara
22 Nov 2019
1#include <stdio.h>
2#include <string.h>
3
4int main() {
5   char str[5][50], temp[50];
6   printf("Enter 5 words: ");
7
8   // Getting strings input
9   for (int i = 0; i < 5; ++i) {
10      fgets(str[i], sizeof(str[i]), stdin);
11   }
12
13   // storing strings in the lexicographical order
14   for (int i = 0; i < 5; ++i) {
15      for (int j = i + 1; j < 5; ++j) {
16
17         // swapping strings if they are not in the lexicographical order
18         if (strcmp(str[i], str[j]) > 0) {
19            strcpy(temp, str[i]);
20            strcpy(str[i], str[j]);
21            strcpy(str[j], temp);
22         }
23      }
24   }
25
26   printf("\nIn the lexicographical order: \n");
27   for (int i = 0; i < 5; ++i) {
28      fputs(str[i], stdout);
29   }
30   return 0;
31}