Step 16: Implement Search by Contact ID

Medium Section 4: Search Module
Solve in Playground →

Problem Statement

Extend the existing search mode framework by implementing the ID-search branch. When search mode 1 is selected, read the requested contact ID and perform a linear search through phonebook from index 0 to contact_count – 1. If the ID is present, print the complete matching contact. If no matching ID exists, print Contact Not Found. The Name-search branch from Task 15 remains intact and continues to display its mode message. All Add, Display, and main-menu code remains unchanged.

Input Format

Complete Add Contact operations may be performed first. Menu choice 3 opens Search, followed by search mode 1 and an integer ID. The application ends with 0.

Output Format

For an existing ID, print Found Contact followed by ID: [id], Name: [name], and Phone: [phone]. For a missing ID, print Contact Not Found. Search mode 2 continues to print Search Mode: Name.

Constraints

The ID search must use linear traversal of the active phonebook records. Only indices 0 through contact_count - 1 may be searched.

Sample Input

1
101
Alice Johnson
1234567890
3
1
101
0

Sample Output

Contact Book Ready.
Phonebook Capacity: 100
Contacts: 0
Menu:
Add
ID: 101
Valid ID
Name: Alice Johnson
Phone: 1234567890
Valid Phone
Contact Added
Menu:
Search
Search Mode: ID
Found Contact
ID: 101
Name: Alice Johnson
Phone: 1234567890
Menu:
Exit

Explanation

The ID branch now performs a linear search over the stored contacts. Because the search mode framework already exists, this functionality can be added without replacing the Name-search branch or any earlier project code.

Starter Code

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

struct Contact {
    int id;
    char name[50];
    char phone[15];
};

struct Contact phonebook[100];
int contact_count = 0;

void add_contact();
void display_contacts();
void search_contact();
void update_contact();
void delete_contact();

void add_contact() {
    if (contact_count >= 100) {
        printf("\nPhonebook Full");
        return;
    }

    int id;

    if (scanf("%d", &id) == 1) {
        printf("\nID: %d", id);

        if (id > 0) {
            printf("\nValid ID");

            {
                int duplicate = 0;

                for (int i = 0; i < contact_count; i++) {
                    if (phonebook[i].id == id) {
                        duplicate = 1;
                        break;
                    }
                }

                if (duplicate) {
                    printf("\nDuplicate ID");
                    return;
                }
            }

            getchar();

            if (fgets(phonebook[contact_count].name,
                      sizeof(phonebook[contact_count].name), stdin) != NULL) {
                phonebook[contact_count].name[
                    strcspn(phonebook[contact_count].name, "\n")
                ] = '\0';

                printf("\nName: %s", phonebook[contact_count].name);

                {
                    char phone_input[100];

                    if (fgets(phone_input, sizeof(phone_input), stdin) != NULL) {
                        phone_input[
                            strcspn(phone_input, "\n")
                        ] = '\0';

                        printf("\nPhone: %s", phone_input);

                        if (strlen(phone_input) >= 1 &&
                            strlen(phone_input) <= 14) {
                            strcpy(phonebook[contact_count].phone, phone_input);
                            printf("\nValid Phone");

                            phonebook[contact_count].id = id;
                            contact_count++;

                            printf("\nContact Added");
                        } else {
                            printf("\nInvalid Phone");
                        }
                    }
                }
            }
        } else {
            printf("\nInvalid ID");
        }
    }
}

void display_contacts() {
    if (contact_count == 0) {
        printf("\nPhonebook Empty");
        return;
    }

    printf("\nID Name Phone");

    for (int i = 0; i < contact_count; i++) {
        printf("\n%d %s %s",
               phonebook[i].id,
               phonebook[i].name,
               phonebook[i].phone);
    }
}

void search_contact() {
    int mode;

    if (scanf("%d", &mode) != 1) {
        return;
    }

    switch (mode) {
        case 1:
            printf("\nSearch Mode: ID");

            // Write your code here
            break;

        case 2:
            printf("\nSearch Mode: Name");
            break;

        default:
            printf("\nInvalid Search Mode");
            break;
    }
}

void update_contact() {
}

void delete_contact() {
}

int main() {
    int choice;

    printf("Contact Book Ready.");
    printf("\nPhonebook Capacity: %d", 100);
    printf("\nContacts: %d", contact_count);

    while (1) {
        printf("\nMenu:");

        if (scanf("%d", &choice) != 1) {
            break;
        }

        switch (choice) {
            case 1:
                printf("\nAdd");
                add_contact();
                break;

            case 2:
                printf("\nDisplay");
                display_contacts();
                break;

            case 3:
                printf("\nSearch");
                search_contact();
                break;

            case 4:
                printf("\nUpdate");
                update_contact();
                break;

            case 5:
                printf("\nDelete");
                delete_contact();
                break;

            case 0:
                printf("\nExit");
                return 0;

            default:
                printf("\nInvalid Choice");
                break;
        }
    }

    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used