Tuesday, January 17, 2012

Sorting Strings in Ascending Order


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n,x;
char str[20][20],str1[20][20];
clrscr();
printf(“enter the number of strings:\n”);
scanf(%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter str[%d],i+1);
scanf(%s”,&str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
x=strcmp(str[i],str[j])
if(x>0)
{
strcpy(str[1],str[j]);
strcpy(str[j],str[i]);
strcpy(str[i],str[1]);
}
}
}
printf(“\nthe sorted strings in ascending order is\n”);
for(i=0;i<n;i++)
{
printf(“\n%s”,str[i]);
}
getch();
}

Output:
enter the number of strings:
3
enter str[1] raja
enter str[2] vignesh
enter str[3] adhi
the sorted strings in ascending order is
adhi
raja
vignesh 

No comments:

Post a Comment