Step 20: Delete the Contact and Shift Remaining Records

Medium Section 5: Update & Delete Module PRO
🔒 Login to Unlock

Problem Statement

Complete the delete_contact() module by extending the record lookup from Task 19. After finding the target contact, shift every subsequent phonebook element one position to the left so the active records remain contiguous. Decrement contact_count by exactly one and print Contact Deleted. If the ID does not exist, retain the existing Contact Not Found behavior. Preserve every previously completed Add, Display, Search, and Update implementation exactly as developed in earlier tasks.

Input Format

One or more complete Add Contact operations may occur first. Menu choice 5 opens Delete, followed by an integer contact ID. The sequence may then use Display or Search to verify the remaining records and finally ends with 0.

Output Format

For an existing record, print the selected contact information followed by Contact Deleted. For a missing ID, print Contact Not Found. Subsequent Display operations must show the remaining contacts in their original relative order.

Constraints

After deletion, contact_count decreases by one. Array elements after the deleted index must shift left exactly once. The deleted record must no longer appear in Display or Search results.

Sample Input

1
101
Alice Johnson
1234567890
1
102
Bob Smith
9876543210
5
101
2
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:
Add
ID: 102
Valid ID
Name: Bob Smith
Phone: 9876543210
Valid Phone
Contact Added
Menu:
Delete
Delete ID: 101
Selected Contact
ID: 101
Name: Alice Johnson
Phone: 1234567890
Contact Deleted
Menu:
Display
ID Name Phone
102 Bob Smith 9876543210
Menu:
Exit

Explanation

Deleting from a fixed array requires more than locating a record. Every later element must move left to close the gap, and contact_count must decrease so Display, Search, and later additions continue using the correct active range.

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");

            {
                int search_id;
                int found = 0;

                if (scanf("%d", &search_id) == 1) {
                    for (int i = 0; i < contact_count; i++) {
                        if (phonebook[i].id == search_id) {
                            found = 1;

                            printf("\nFound Contact");
                            printf("\nID: %d", phonebook[i].id);
                            printf("\nName: %s", phonebook[i].name);
                            printf("\nPhone: %s", phonebook[i].phone);
                            break;
                        }
                    }

                    if (!found) {
                        printf("\nContact Not Found");
                    }
                }
            }
            break;

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

            {
                char search_name[50];
                int found = 0;

                getchar();

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

                    for (int i = 0; i < contact_count; i++) {
                        if (strcmp(phonebook[i].name, search_name) == 0) {
                            found = 1;

                            printf("\nFound Contact");
                            printf("\nID: %d", phonebook[i].id);
                            printf("\nName: %s", phonebook[i].name);
                            printf("\nPhone: %s", phonebook[i].phone);
                            break;
                        }
                    }

                    if (!found) {
                        printf("\nContact Not Found");
                    }
                }
            }
            break;

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

void update_contact() {
    int update_id;
    int found = 0;

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

    for (int i = 0; i < contact_count; i++) {
        if (phonebook[i].id == update_id) {
            found = 1;

            printf("\nUpdate ID: %d", update_id);

            getchar();

            {
                char new_phone[100];

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

                    printf("\nNew Phone: %s", new_phone);

                    if (strlen(new_phone) >= 1 &&
                        strlen(new_phone) <= 14) {
                        strcpy(phonebook[i].phone, new_phone);
                        printf("\nPhone Updated");
                    } else {
                        printf("\nInvalid Phone");
                    }
                }
            }

            break;
        }
    }

    if (!found) {
        printf("\nContact Not Found");
    }
}

void delete_contact() {
    int delete_id;
    int found_index = -1;

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

    printf("\nDelete ID: %d", delete_id);

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

    if (found_index == -1) {
        printf("\nContact Not Found");
        return;
    }

    printf("\nSelected Contact");
    printf("\nID: %d", phonebook[found_index].id);
    printf("\nName: %s", phonebook[found_index].name);
    printf("\nPhone: %s", phonebook[found_index].phone);

    // Write your code here
}

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