Create an Contact Management System in C Language with Free Codes

Table of Contents

Introduction – Contact Management

Hello guys, here we will create a Contact Management System with C Language. This is the one of the advance project topic in Project List. With this project you master in if-else statement, Structures, Functions and Switch case Statements. So Let’s Start the Creation of the Project.

Feature of the Project

Here is the feature of the Project which is include in the project,

  1. Add Contact
  2. Display Contact
  3. Search Contact
  4. Delete Contact

Software Use for Output

For get the output of the Contact Management System we used the  Code::Blocks software the beginner friendly and mostly used software by the C Language Developers.

Free E-Books

Output – Contact Management System

Project Explanation – Contact Management System

Here we explain the project codes in parts to understand it briefly,

1 – Define Header File

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

Here we define our Header files…

2 – Define Structure

// Define structure for Contact
struct Contact {
    char name[50];
    char phone[20];
    char email[50];
};

The ‘struct Contact’ defines a structure to store contact information, like name, phone number, and email address.

3 – Function to Add New Contact

// Function to add a new contact
void addContact(struct Contact *contacts, int *numContacts) {
    printf("Enter name: ");
    scanf("%s", contacts[*numContacts].name);
    printf("Enter phone number: ");
    scanf("%s", contacts[*numContacts].phone);
    printf("Enter email address: ");
    scanf("%s", contacts[*numContacts].email);
    (*numContacts)++;
    printf("Contact added successfully.\n");
}

Here we write a function named ‘addContact’ which is used for add the new Contact to the system. It prompts the user to enter the Name of the Contact, E-mail Address, and Phone Number and then stores the information in the next available slot in the ‘contacts’ array.

Free Hand-Written Notes

4 – Function to Display all Contacts

// Function to display all contacts
void displayContacts(struct Contact *contacts, int numContacts) {
    printf("\nContacts:\n");
    for (int i = 0; i < numContacts; i++) {
        printf("Name: %s\n", contacts[i].name);
        printf("Phone: %s\n", contacts[i].phone);
        printf("Email: %s\n", contacts[i].email);
        printf("\n");
    }
}

‘displayContacts’ – This Function displays all contacts stored in the system. It iterates through the ‘contacts’ array and prints the name, phone number, and e-mail address of each contact.

5 – Function to Search Contact

// Function to search for a contact by name
void searchContact(struct Contact *contacts, int numContacts, char *searchName) {
    int found = 0;
    for (int i = 0; i < numContacts; i++) {
        if (strcmp(contacts[i].name, searchName) == 0) {
            printf("Contact found:\n");
            printf("Name: %s\n", contacts[i].name);
            printf("Phone: %s\n", contacts[i].phone);
            printf("Email: %s\n", contacts[i].email);
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("Contact not found.\n");
    }
}

Here we declare the ‘seacrhContact’ function. Which is used for search the contact by name. It takes the name to search for as input and iterates through the ‘contacts’ array to find a match. If a contact with the specified name is found, its details are displayed.

6 – Function to Delete Contacts

// Function to delete a contact by name
void deleteContact(struct Contact *contacts, int *numContacts, char *deleteName) {
    int found = 0;
    for (int i = 0; i < *numContacts; i++) {
        if (strcmp(contacts[i].name, deleteName) == 0) {
            // Shift elements to the left to overwrite the deleted contact
            for (int j = i; j < *numContacts - 1; j++) {
                strcpy(contacts[j].name, contacts[j + 1].name);
                strcpy(contacts[j].phone, contacts[j + 1].phone);
                strcpy(contacts[j].email, contacts[j + 1].email);
            }
            (*numContacts)--;
            found = 1;
            printf("Contact deleted successfully.\n");
            break;
        }
    }
    if (!found) {
        printf("Contact not found.\n");
    }
}

‘deleteContact’ – This Function is used for delete the contact number by name. It takes the name of the contact to delete as input, searches for the contact in the ‘contacts’ array, and removes it by shifting the remaining contacts to the left to fill the gap.

7 – Main Function

7.1 Variable Declaration
int main() {
    struct Contact contacts[100];
    int numContacts = 0;
    int choice;
    char searchName[50];
    char deleteName[50];

    // Loop is here 

            //Switch Case Statement

   // Loop is here 

    return 0;
}
  • struct Contact contacts[100]; – An array of ‘Contac’ structures to store contact information. It has capacity to store up to 100 contacts.
  • int numContacts = 0; – An integer variable to keep track of the number of contacts currently stored in the system.
  • int choice; – An integer variable to store the user’s choice from the menu options.
  • char searchName[50]; – A character array to store the name entered by the user for searching contacts.
  • char deleteName[50]; – A character array to store the name entered by the user for deleting a contact.
7.2 Menu Driven Interface
do {
        printf("\nContact Management System\n");
        printf("1. Add Contact\n");
        printf("2. Display Contacts\n");
        printf("3. Search Contact\n");
        printf("4. Delete Contact\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        // Switch Case Statement 

    } while (choice != 5);

This Program present the menu to the user using ‘printf’ statements inside a ‘do-while’ loop. This loop ensures that the menu is displayed at least once and continues to prompt the user until they choose to exit the program (Option – 5).

The Manu Option Includes:

  1. Option 1 : Add Contact
  2. Option 2 : Display Contact
  3. Option 3 : Search Contact
  4. Option 4 : Delete Contact
  5. Option 5 : Exit

After displaying the menu, the program waits for the user to input their choice using ‘scanf’ and stores in the ‘choice’ variable.

7.3 Switch Statement
switch (choice) {
            case 1:
                addContact(contacts, &numContacts);
                break;
            case 2:
                displayContacts(contacts, numContacts);
                break;
            case 3:
                printf("Enter name to search: ");
                scanf("%s", searchName);
                searchContact(contacts, numContacts, searchName);
                break;
            case 4:
                printf("Enter name to delete: ");
                scanf("%s", deleteName);
                deleteContact(contacts, &numContacts, deleteName);
                break;
            case 5:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice.\n");
        }
    }

The Switch Statement is used for execute different codes block on the user’s choice.

Like For each menu option, the corresponding function is called:

  • If the user chooses option 1, the ‘addContact’ function is called to add a new contact.
  • If the user chooses option 2, the ‘displayContacts’ function is called to display all contacts.
  • If the user chooses option3, the program prompts the user to enter a name to search for, the calls the ‘searchContact’ function to search for the contact.
  • If the user chooses option 4, the program prompts the user to enter a name to delete , the calls the ‘deleteContact’ function to delete the contact.
  • if the user chooses option 5, the program prints a message indicating that it is exiting.
  • If the User enters a invalid choice, the program prints “Invalid Choice” and displays the menu again.

Complete Project Codes

Here is the complete code of “Contact Management System”,

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

// Define structure for Contact
struct Contact {
    char name[50];
    char phone[20];
    char email[50];
};

// Function to add a new contact
void addContact(struct Contact *contacts, int *numContacts) {
    printf("Enter name: ");
    scanf("%s", contacts[*numContacts].name);
    printf("Enter phone number: ");
    scanf("%s", contacts[*numContacts].phone);
    printf("Enter email address: ");
    scanf("%s", contacts[*numContacts].email);
    (*numContacts)++;
    printf("Contact added successfully.\n");
}

// Function to display all contacts
void displayContacts(struct Contact *contacts, int numContacts) {
    printf("\nContacts:\n");
    for (int i = 0; i < numContacts; i++) {
        printf("Name: %s\n", contacts[i].name);
        printf("Phone: %s\n", contacts[i].phone);
        printf("Email: %s\n", contacts[i].email);
        printf("\n");
    }
}

// Function to search for a contact by name
void searchContact(struct Contact *contacts, int numContacts, char *searchName) {
    int found = 0;
    for (int i = 0; i < numContacts; i++) {
        if (strcmp(contacts[i].name, searchName) == 0) {
            printf("Contact found:\n");
            printf("Name: %s\n", contacts[i].name);
            printf("Phone: %s\n", contacts[i].phone);
            printf("Email: %s\n", contacts[i].email);
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("Contact not found.\n");
    }
}

// Function to delete a contact by name
void deleteContact(struct Contact *contacts, int *numContacts, char *deleteName) {
    int found = 0;
    for (int i = 0; i < *numContacts; i++) {
        if (strcmp(contacts[i].name, deleteName) == 0) {
            // Shift elements to the left to overwrite the deleted contact
            for (int j = i; j < *numContacts - 1; j++) {
                strcpy(contacts[j].name, contacts[j + 1].name);
                strcpy(contacts[j].phone, contacts[j + 1].phone);
                strcpy(contacts[j].email, contacts[j + 1].email);
            }
            (*numContacts)--;
            found = 1;
            printf("Contact deleted successfully.\n");
            break;
        }
    }
    if (!found) {
        printf("Contact not found.\n");
    }
}

int main() {
    struct Contact contacts[100];
    int numContacts = 0;
    int choice;
    char searchName[50];
    char deleteName[50];

    do {
        printf("\nContact Management System\n");
        printf("1. Add Contact\n");
        printf("2. Display Contacts\n");
        printf("3. Search Contact\n");
        printf("4. Delete Contact\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addContact(contacts, &numContacts);
                break;
            case 2:
                displayContacts(contacts, numContacts);
                break;
            case 3:
                printf("Enter name to search: ");
                scanf("%s", searchName);
                searchContact(contacts, numContacts, searchName);
                break;
            case 4:
                printf("Enter name to delete: ");
                scanf("%s", deleteName);
                deleteContact(contacts, &numContacts, deleteName);
                break;
            case 5:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice.\n");
        }
    } while (choice != 5);

    return 0;
}

Conclusion

Here we learn about how the Contact Management System is designed and made with C Language With the use of C Language Concepts like Loops, Functions, Structure, and Switch case.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top