Thursday, November 27, 2014

C Program to sort the list of strings using built in string function

/*.....................................................................................................................
File : rec18.c
program : Program to sort the list of strings using built in string function
author : sujithbaby440@gmail.com
Date : 1-11-14
.........................................................................................................................*/
#include<stdio.h>
#include<string.h>
main()
{
int i,j,n;
char temp[30],names[20][30];
system("clear");
printf("Program to sort the list of name\n");
printf("Enter the number of names\n");
scanf("%d",&n);
printf("Enter names\n",n);
for(i=1;i<=n;i++){
scanf("%s",names[i]);
}
for(i=1;i<=n-1;i++){
for(j=1;j<=n-i;j++){
if(strcmp(names[j],names[j+1])>0){
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}
}
printf("\nSorted list:\n\n");
for(i=1;i<=n;i++){
printf(" %s\n",names[i]);
}
}

OUTPUT 1
Program to sort the list of name
Enter the number of names
6
Enter names
renjith
sujith
arunjith
abhijith
anand
amaljyothi
Sorted list:
abhijith
amaljyothi
anand
arunjith
renjith
sujith
[141740@localhost ~]$

OUTPUT 2
Program to sort the list of name
Enter the number of names
5
Enter names
abdul
shanoob
sujith
nihal
rishad
Sorted list:
abdul
nihal
rishad
shanoob
sujith
[141740@localhost ~]$
/*.............................................................................................................................

No comments:

Post a Comment