20_10

Write a C program which sort strings in lexicographical order.

 Source code:

//Write a C program which sort strings in lexicographical order.


#include<stdio.h>

#include <string.h>

int main()

{

    int i, j;

    char str[10][50], temp[50];

printf("Enter a string: ");

    /* Read 10 elements */

    for(i=0; i<10; ++i)

        scanf("%s[^\n]",str[i]);

for(i=0; i<9; ++i)

        for(j=i+1; j<10 ; ++j)

        {

            if(strcmp(str[i], str[j])>0)

            {

                strcpy(temp, str[i]);

                strcpy(str[i], str[j]);

                strcpy(str[j], temp);

            }

        }

printf("In Lexicographical Order\n");

    for(i=0; i<10; ++i)

    {

        puts(str[i]);

    }

    return 0;

}

Output:
C program which sort strings in lexicographical order


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...