C Program to sort names alphabetically using structure


March 2, 2022, Learn eTutorial
1991

What is the structure datatype in C?

The structure is a user-defined data type in C. It is a set of elements with different data types represented by a single name stored in the sequence of a memory location. We use structure to keep records of other data type elements. We use the keyword struct to define a structure. An example of the structure is :

  struct person
    {
        char name[10];
        int rno;
    };

How to sort the elements in a structure list in C?

To implement the sorting of the names of students in alphabetical order C program we have to import the header files. Now initialize a structure person and define the function sort(int n).

Read the number of students into the variable no. Then we accept the student's name and roll number into stud[i].Name, stud[i].Rollno using for loop. Then we display the list of student's names and roll numbers using for loop and printf function.

Call the sort function to sort the elements in the list using the strcmp function and temp variable. After sorting, we will display the sorted list using for loop in the main program. Inside the sort() function, we compare each name with the next name using the strcmp() function and swap according to the criteria we need.

Note: We use string.h Header file in the program to use the strcmp function, which is defined in the string.h Library.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Include the header file string.h.

STEP 3: Define the structure person with a name, no as structure members.

STEP 4: Create the object for the structure person as NAME.

STEP 5: Declare stud[10],temp[10] as type NAME.

STEP 6: Declare the variables no, 'i' as an integer.

STEP 7: Declare the function void sort(int N).

STEP 8: Read the number of students into variable no.

STEP 9: By using a for loop read the name of the person into the variable stud[i].name and read the roll number into the variable stud[i].rno and assign temp[i]=stud[i].

STEP 10: Display Names before sorting using printf function.

STEP 11: Display the list using for loop with the condition i

STEP 12:Then call the function sort(no) to sort the names.

STEP 13: Display the names after sorting as stud[i].name,stud[i].rno using for loop.

Function void sort(int N)

STEP 1: Declare the variable i, j as an integer.

STEP 2: Declare the variable temp as a type of NAME.

STEP 3: By using netsed for loop ,compare srtd[i].name and stud[j].name,if std[i[.name is > std[j].name then swap it.


To implement this sorting program, we are using the below C concepts. We recommend reading those topics for a better understanding

C Source Code

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

struct person {
  char name[10]; /* Using structure to save the names */
  int rno;
};

typedef struct person NAME; /* creating object for using structure */
NAME stud[10], temp[10];
void main() {
  int no, i;
  void sort(int N); /* Function declaration */
  fflush(stdin);
  printf("Enter the number of students in the list\n"); /* enters the number of students */
  scanf("%d", & no);
  for (i = 0; i < no; i++) {
    printf("\n Enter the name of  person %d : ", i + 1);
    fflush(stdin);
    scanf("%s", & stud[i].name);
    printf("Enter the roll number of %d : ", i + 1); /* enters and accepting the name and roll number */
    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=%d\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=%d\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() */
                                      

OUTPUT

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