C program to accept a set of names and sort them in an alphabetical order, Use structures to store the names

by Nideesh C on April 13, 2011 · 0 comments

in C Programming




/* C program to accept a set of names and sort them in an alphabetical order, Use structures to store the names */

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

struct person
{
char name[10];
int rno;
};
typedef struct person NAME;
NAME  stud[10], temp[10];

void main()
{
int no,i;

void sort(int N);     /* Function declaration */

clrscr();
fflush(stdin);

printf(“Enter the number of students in the list\n”);
scanf(“%d”,&no);

for(i = 0; i < no; i++)
{
printf(“\nEnter the name of  person %d : “, i+1);
fflush(stdin);
gets(stud[i].name);

printf(“Enter the roll number of %d : “, i+1);
scanf(“%d”,&stud[i].rno);
temp[i] = stud[i];
}

printf(“\n*****************************\n”);
printf (“     Names before sorting     \n”);
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf(“%-10s\t%3d\n”,temp[i].name,temp[i].rno);
}

sort(no);       /* Function call */

printf(“\n*****************************\n”);
printf (“     Names after sorting     \n”);
printf(“\n*****************************\n”);
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf(“%-10s\t%3d\n”,stud[i].name,stud[i].rno);

}
printf(“\n*****************************\n”);
}          /* End of main() */

/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;

for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp    = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
}          /* end of sort() */

/*——————————————————–
Enter the number of students in the list
5

Enter the name of  person 1 : Rajashree
Enter the roll number of 1 : 123

Enter the name of  person 2 : John
Enter the roll number of 2 : 222

Enter the name of  person 3 : Priya
Enter the roll number of 3 : 331

Enter the name of  person 4 : Anand
Enter the roll number of 4 : 411

Enter the name of  person 5 : Nirmala
Enter the roll number of 5 : 311

*****************************
Names before sorting
Rajashree       123
John            222
Priya           331
Anand           411
Nirmala         311

*****************************
Names after sorting

*****************************
Anand           411
John            222
Nirmala         311
Priya           331
Rajashree       123

*****************************
—————————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the give names and the sorted names in two columns side by side with suitable heading
  2. C program to accept a amtric of order MxN and sort all rows of the matrix in ascending order and all columns in descendng order
  3. C program to accept a set of numbers and arrange them in a descending order
  4. C program to sort N numbers in ascending order array with suitable headings
  5. C program to sort given N elements using SELECTION sort method using functions a) To find maximum of elements b) To swap two elements

Leave a Comment

Previous post:

Next post: