Easy Tutorial
❮ C Function Wctomb C Function Strncmp ❯

C Language Example - String Sorting

C Language Examples

Sorting in dictionary order.

Example

#include<stdio.h>
#include <string.h>

int main()
{
    int i, j;
    char str[10][50], temp[50];

    printf("Enter 10 words:\n");

    for(i=0; i&lt;10; ++i) {
        scanf("%s[^\n]",str[i]);
    }

    for(i=0; i&lt;9; ++i) {
        for(j=i+1; j&lt;10 ; ++j)
        {
            if(strcmp(str[i], str[j])>0)
            {
                strcpy(temp, str[i]);
                strcpy(str[i], str[j]);
                strcpy(str[j], temp);
            }
        }
    }

    printf("\nSorted order: \n");
    for(i=0; i&lt;10; ++i)
    {
        puts(str[i]);
    }

    return 0;
}

Output:

Enter 10 words:
C
C++
Java
PHP
Python
Perl
Ruby
R
JavaScript
PHP

Sorted order: 
C
C++
Java
JavaScript
PHP
PHP
Perl
Python
R
Ruby

C Language Examples

❮ C Function Wctomb C Function Strncmp ❯