C program to sort names in alphabetical order using structures

Structure is a collection of different datatype variables, grouped together under a single name. In this article, we will learn how to sort names in alphabetical order using structures in C.

Syntax

struct structure_name {
    datatype member1;
    datatype member2;
    // ... more members
};

Features of Structure

The features of structure in the C programming language are as follows −

  • It is possible to copy the contents of all the structure elements of different datatypes to another structure variable of its type by using an assignment operator.

  • For handling the complex datatypes, it is better to create structure within another structure, which is called nested structures.

  • It is possible to pass an entire structure, individual elements of structure and an address of structure to a function.

  • It is possible to create structure pointers.

Declaration and Initialization of Structures

General form of structure declaration is as follows −

struct tagname {
    datatype member1;
    datatype member2;
    datatype member_n;
};

Here,

  • struct is the keyword.
  • tagname specifies name of structure.
  • member1, member2 are the data items.

For example,

struct book {
    int pages;
    char author[30];
    float price;
};

Example: Sorting Names Alphabetically

Following is the C program to sort the names in an alphabetical order by using the structures −

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

struct student {
    char name[50];
    int rno;
};

void sortNames(struct student s[], int n) {
    int i, j;
    struct student temp;
    
    for(i = 0; i < n - 1; i++) {
        for(j = i + 1; j < n; j++) {
            if(strcmp(s[i].name, s[j].name) > 0) {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }
}

int main() {
    struct student s[5];
    int n, i;
    
    printf("Enter the number of students: ");
    scanf("%d", &n);
    
    for(i = 0; i < n; i++) {
        printf("Enter name of student %d: ", i + 1);
        scanf("%s", s[i].name);
        printf("Enter roll number: ");
        scanf("%d", &s[i].rno);
    }
    
    sortNames(s, n);
    
    printf("\nStudents sorted by name:
"); for(i = 0; i < n; i++) { printf("%s\t%d
", s[i].name, s[i].rno); } return 0; }

Output

When the above program is executed, it produces the following result −

Enter the number of students: 5
Enter name of student 1: Priya
Enter roll number: 3
Enter name of student 2: Hari
Enter roll number: 5
Enter name of student 3: Pinky
Enter roll number: 7
Enter name of student 4: Lucky
Enter roll number: 1
Enter name of student 5: Krishna
Enter roll number: 2

Students sorted by name:
Hari    5
Krishna 2
Lucky   1
Pinky   7
Priya   3

How It Works

The program uses bubble sort algorithm to sort the names:

  • The strcmp() function compares two strings lexicographically
  • If strcmp(s1, s2) > 0, then s1 comes after s2 alphabetically
  • When this condition is true, we swap the entire structure elements
  • This process continues until all names are sorted in alphabetical order

Conclusion

Sorting names using structures demonstrates how to work with user-defined data types in C. The combination of structures and string comparison functions makes it easy to organize and sort complex data efficiently.

Updated on: 2026-03-15T14:17:16+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements