Tuesday 17 March 2015

To sort the given strings in alphabetical order Using C


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

void main()
{
char str[10][20], temp[20] ;
int n, i, j ;
clrscr() ;
printf("Enter the number of strings : ") ;
scanf("%d", &n) ;
printf("\nEnter the strings : \n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%s", str[i]) ;
for(i = 0 ; i < n - 1 ; i++)
for(j = 0 ; j < n - 1; j++)
if(strcmp(str[j], str[j + 1]) > 0)
{
strcpy(temp, str[j]) ;
strcpy(str[j], str[j + 1]) ;
strcpy(str[j + 1], temp) ;
}
printf("\nThe sorted order of strings are : \n\n") ;
for(i = 0 ; i < n ; i++)
printf("%s \n", str[i]) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the number of strings : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish
The sorted order of strings are :
bhuvan
priya
satish
udaya
viji

No comments:

Post a Comment