Program to sort string in dictionary order

Program to sort string in dictionary order

#include<stdio.h>
#include<string.h>
main()
{
int i,j;
char str[10][50],temp[50];
printf("enter 10 words:\n");
for(i=0;i<10;i++)
scanf("%s[^\n]",str[i]);
for(i=0;i<10;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("\n In lexicographical order:");
for(i=0;i<10;i++)
{
puts(str[i]);
}
}

OUTPUT:

Enter 10 words:
C
C++
java
PHP
python
javascript
R
Perl
Ruby
PHP
In lexicographical order:
C
C++
java
javascript
PHP
PHP
Perl
Python
R
Ruby

0 Comments