C Program to Sort Elements in Lexicographical Order (Dictionary Order) Using For Loop

#include <stdio.h>
#include <string.h>
int main() {
   char str[5][50], temp[50];
   printf("Enter 5 words: ");
   // Getting strings input
   for (int i = 0; i < 5; ++i) {
      fgets(str[i], sizeof(str[i]), stdin);
   }
   // storing strings in the lexicographical order
   for (int i = 0; i < 5; ++i) {
      for (int j = i + 1; j < 5; ++j) {
         // swapping strings if they are not in the lexicographical order
         if (strcmp(str[i], str[j]) > 0) {
            strcpy(temp, str[i]);
            strcpy(str[i], str[j]);
            strcpy(str[j], temp);
         }
      }
   }
   printf("\nIn the lexicographical order: \n");
   for (int i = 0; i < 5; ++i) {
      fputs(str[i], stdout);
   }
   return 0;
}

Output:

Enter 5 words: R programming
JavaScript
JIRA
C programming
C# programming
In the lexicographical order:
C programming
C# programming
JavaScript
JIRA
R programming

Leave a Comment