Monday, October 31, 2011

Sorting the String in Dictionary order

Given an Array of Strings Write a Program to Sort the String in Dictionary Order.


Program:



#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[25][25],t[25];
int i,j ,n;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements:\n");
for(i=0;i<n;i++)
scanf("%s",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("the sorted array:\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
getch();
}

1 comments:

Unknown said...

if(strcmp(a[i],a[j])>0)
What is d use of this ND why only 0 is used

Post a Comment

About Me