---
title: "Sort names and addresses alphabetically"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-names-alphabetical"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-names-alphabetical
site: BCT Engineering
---

# Sort names and addresses alphabetically

Computer Programming · Chapter 8: Structures

- Asked in: 2071 Chaitra, 4 marks
- Video: [C program to SORT NAMES in ALPHABETICAL ORDER | Sorting names in  C language](https://www.youtube.com/watch?v=A28lN86quGY) — Krishna Lede
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-names-alphabetical
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Write a C program that reads several different names and address using structure computer, rearrange the names into alphabetical order and write out alphabetical list.

## Short answer

Read names and addresses into struct computer array. Sort by name using strcmp and swapping whole structures. Print the alphabetically ordered list.

## Answer

**Design.** A structure `computer` (as named in the question) has `name` and `address`. Several entries (5 here) are read into an array of structures. The array is sorted on `name` with `strcmp()`; when the names are out of order the whole structures are swapped, so each address stays with its name. The sorted list is printed.

```c
#include <stdio.h>
#include <string.h>
#define N 5

struct computer {
    char name[30];
    char address[40];
};

int main(void)
{
    struct computer c[N], t;

    for (int i = 0; i < N; i++) {
        printf("Name and address %d: ", i + 1);
        scanf("%29s %39s", c[i].name, c[i].address);
    }

    for (int i = 0; i < N - 1; i++)           /* sort by name */
        for (int j = i + 1; j < N; j++)
            if (strcmp(c[i].name, c[j].name) > 0) {
                t = c[i];
                c[i] = c[j];
                c[j] = t;
            }

    printf("\nAlphabetical list\n");
    for (int i = 0; i < N; i++)
        printf("%-10s %s\n", c[i].name, c[i].address);
    return 0;
}
```

**Sample run**

Input (entered one after another): `Ram Kathmandu Bibek Pokhara Sita Lalitpur Anil Butwal Hari Bhaktapur`

```text
Alphabetical list
Anil       Butwal
Bibek      Pokhara
Hari       Bhaktapur
Ram        Kathmandu
Sita       Lalitpur
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which function compares names?
   A. strcmp()
   B. strlen()
   C. strcat()
   D. strcpy() as comparison
   Answer: A. strcmp returns > 0 when the first is larger. (Key point: Comparison)

2. Why swap whole structures?
   A. To save memory
   B. So each address stays with its own name
   C. Because strcmp requires it
   D. To change the tag
   Answer: B. Swapping only names would mismatch addresses. (Key point: Swap)

3. When is a swap done?
   A. When < 0
   B. When == 0
   C. When strcmp(c[i].name, c[j].name) > 0
   D. Always
   Answer: C. The earlier name is alphabetically later. (Key point: Condition)

4. What is the first name in the sorted sample?
   A. Ram
   B. Sita
   C. Bibek
   D. Anil
   Answer: D. Anil comes first alphabetically. (Key point: Sample)

## Related in Computer Programming

- [Rewrite string characters alphabetically](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabetical-order) — Computer Programming, chapter 7
- [Steps to develop a computer program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asteps-develop-program) — Computer Programming, chapter 2, asked 6×
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Declaration vs definition; C program structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adeclaration-definition-structure-of-c) — Computer Programming, chapter 3
- [Sort employee structures by salary](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-sort-salary) — Computer Programming, chapter 8, asked 2×
- [Computer program and programming](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-program-programming) — Computer Programming, chapter 2

## Related in other subjects

- [Cache management; direct vs associative address structure](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Adirect-vs-assoc-address) — Computer Organization and Architecture, chapter 6
- [Program for RK-4 first order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-first-order) — Numerical Method, chapter 6
- [Program for RK-4 second order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-second-order) — Numerical Method, chapter 6
- [Interconnection structure of computer](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Ainterconnection-structure) — Computer Organization and Architecture, chapter 1, asked 3×
