C Program to Implement Lexical Sorting
This is the c program to sort strings (lexical sorting) given n number of strings it will sort the strings in alphabetical order
#include<string.h>
#include<stdio.h>
main()
{
char n[100][100];
int i=0,item;
printf("ENTER THE NUMBER OF ITEMS TO BE SORTEDn");
scanf("%d",&item);
printf("ENTER THE ITEMS TO BE SORTEDn");
while(i<item){
scanf("%s",n[i++]);}
lexi(n,item);
}
void lexi(char str[20][20],int num)
{
int i,j;
char temp[20];
for(i=1;i<num;i++)
{
for(j=1;j<=num-i;j++)
{
if(strcmp(str[j-1],str[j])>0)
{
strcpy(temp,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],temp);
}
}
}
printf("nTHE SORTED LISTn");
for(i=0;i<num;i++)
{
printf("%sn",str[i]);
}
}
Recent Comments