/* * Program to Sort Elements in Lexicographical Order (Dictionary Order) * * https://www.programiz.com/c-programming/examples/lexicographical-order */ #include #include /* Entry point to program */ int main() { char str[5][20], temp[20]; int i; int j; printf("Enter 5 strings:\n"); /* Getting strings input */ for (i = 0; i < 5; ++i) { gets(str[i]); } puts(""); /* Sorting strings in the lexicographical order */ for (i = 0; i < 5; ++i) { for (j = i + 1; j < 5; ++j) { if (strcmp(str[i], str[j]) > 0) { printf("%s<->%s ", str[i], str[j]); strcpy(temp, str[i]); strcpy(str[i], str[j]); strcpy(str[j], temp); } } puts(""); printf("%s, %s, %s, %s, %s\n", str[0], str[1], str[2], str[3], str[4]); } printf("In the lexicographical order:\n"); for (i = 0; i < 5; ++i) { puts(str[i]); } return 0; }