Contact Management System (CMS)

 
Contact Management System project using C with source code


Contact Management System (CMS) in C

A Contact Management System (CMS) is an essential application for organizing and managing contacts efficiently. Here is a simple implementation using C that allows users to perform core operations such as adding, viewing, searching, modifying, and deleting contacts through a straightforward console interface.

Note for Beginners: This project offers practical exposure to foundational C concepts, including structs, arrays of structures, file handling, and string manipulation routines.

Project Overview

The Contact Management System is structured to capture and manage three key contact attributes:

  • Name: Full name of the contact.
  • Phone Number: Primary phone number.
  • Email Address: Valid email address.

Using this data model, the program provides six primary operations:

  1. Add New Contact
  2. View All Contacts
  3. Search for a Contact
  4. Modify a Contact
  5. Delete a Contact
  6. Exit the Program

Programming Concepts Used

Concept Implementation Details
Structures (struct) Groups related attributes (name, phone, email) into a single composite entity.
Arrays Maintains an array of structures to store multiple contact entries in memory.
String Handling Uses functions like fgets(), strcspn(), and strcasecmp() to safely read and sanitize inputs.
Control Flow Utilizes switch-case statements and while loops to drive interactive console menus.
Modular Programming Separates concerns across distinct, specialized functions for easier maintenance and debugging.

Features and Functions Breakdown

1. Add Contact

Function: addContact()

Prompts the user for the contact's name, phone number, and email. Provided the contact list has not hit its maximum limit (100 contacts), the new record is appended to the contacts array.

2. View All Contacts

Function: viewContacts()

Iterates through the stored list and prints each contact's details in a clean, numbered sequence. If no records exist, it outputs a descriptive empty notice.

3. Search Contact

Function: searchContact()

Performs a case-insensitive linear search across contact names using strcasecmp(). When a match is found, the details are printed immediately; otherwise, a Contact not found message appears.

4. Modify Contact

Function: modifyContact()

Locates a record by name and prompts the user to enter updated values for their name, phone number, and email, overwriting the existing slot in place.

5. Delete Contact

Function: deleteContact()

Locates a target contact, shifts all subsequent array items forward by one index to preserve order, and decrements the total contactCount.

6. Exit Program

Function: exit(0)

Terminates execution safely from the main menu loop.

Basic In-Memory Implementation

This starter version keeps all records in temporary memory during execution:

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

// Define a structure for the contact information
struct Contact {
    char name[50];
    char phone[15];
    char email[50];
};

// Function prototypes
void addContact();
void viewContacts();
void searchContact();
void modifyContact();
void deleteContact();
void displayMenu();

// Global variables
struct Contact contacts[100];
int contactCount = 0;

int main() {
    int choice;
    while (1) {
        displayMenu();
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        getchar(); // Clear the input buffer

        switch (choice) {
            case 1:
                addContact();
                break;
            case 2:
                viewContacts();
                break;
            case 3:
                searchContact();
                break;
            case 4:
                modifyContact();
                break;
            case 5:
                deleteContact();
                break;
            case 6:
                printf("\nExiting...\n");
                exit(0);
                break;
            default:
                printf("\nInvalid choice! Please try again.\n");
        }
    }
    return 0;
}

void displayMenu() {
    printf("\n*** Contact Management System ***\n");
    printf("1. Add New Contact\n");
    printf("2. View All Contacts\n");
    printf("3. Search Contact\n");
    printf("4. Modify Contact\n");
    printf("5. Delete Contact\n");
    printf("6. Exit\n");
}

void addContact() {
    if (contactCount < 100) {
        struct Contact newContact;
        printf("\nEnter Name: ");
        fgets(newContact.name, sizeof(newContact.name), stdin);
        newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove newline
        printf("Enter Phone Number: ");
        fgets(newContact.phone, sizeof(newContact.phone), stdin);
        newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; // Remove newline
        printf("Enter Email: ");
        fgets(newContact.email, sizeof(newContact.email), stdin);
        newContact.email[strcspn(newContact.email, "\n")] = '\0'; // Remove newline

        contacts[contactCount++] = newContact;
        printf("\nContact added successfully!\n");
    } else {
        printf("\nContact list is full!\n");
    }
}

void viewContacts() {
    if (contactCount == 0) {
        printf("\nNo contacts available.\n");
        return;
    }

    printf("\n*** Contact List ***\n");
    for (int i = 0; i < contactCount; i++) {
        printf("Contact %d:\n", i + 1);
        printf("Name: %s\n", contacts[i].name);
        printf("Phone: %s\n", contacts[i].phone);
        printf("Email: %s\n\n", contacts[i].email);
    }
}

void searchContact() {
    char searchName[50];
    printf("\nEnter the name to search: ");
    fgets(searchName, sizeof(searchName), stdin);
    searchName[strcspn(searchName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, searchName) == 0) {
            printf("\nContact 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("\nContact not found.\n");
    }
}

void modifyContact() {
    char modifyName[50];
    printf("\nEnter the name of the contact to modify: ");
    fgets(modifyName, sizeof(modifyName), stdin);
    modifyName[strcspn(modifyName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, modifyName) == 0) {
            printf("\nModifying Contact:\n");
            printf("Enter new name: ");
            fgets(contacts[i].name, sizeof(contacts[i].name), stdin);
            contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0';
            printf("Enter new phone number: ");
            fgets(contacts[i].phone, sizeof(contacts[i].phone), stdin);
            contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0';
            printf("Enter new email: ");
            fgets(contacts[i].email, sizeof(contacts[i].email), stdin);
            contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0';

            printf("\nContact modified successfully!\n");
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("\nContact not found.\n");
    }
}

void deleteContact() {
    char deleteName[50];
    printf("\nEnter the name of the contact to delete: ");
    fgets(deleteName, sizeof(deleteName), stdin);
    deleteName[strcspn(deleteName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, deleteName) == 0) {
            for (int j = i; j < contactCount - 1; j++) {
                contacts[j] = contacts[j + 1];
            }
            contactCount--;
            printf("\nContact deleted successfully!\n");
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("\nContact not found.\n");
    }
}

Sample Output

*** Contact Management System ***
1. Add New Contact
2. View All Contacts
3. Search Contact
4. Modify Contact
5. Delete Contact
6. Exit

Enter your choice: 1

Enter Name: dailyaspirants
Enter Phone Number: 4567893211
Enter Email: kumar@gmail.com

Contact added successfully!

*** Contact List ***
Contact 1:
Name: dailyaspirants
Phone: 4567893211
Email: kumar@gmail.com

Advantages & Limitations

Advantages Limitations
User-Friendly: Clear console prompts make testing and interaction straightforward. Fixed Size: Constrained to 100 entries via static arrays instead of dynamic memory.
Clean Architecture: Distinct functions streamline troubleshooting and updates. Volatile Storage: Basic version loses all data once the program exits.
Fast Search: Case-insensitive linear lookup works smoothly for small contact pools. Terminal Bound: Lacks a graphical interface or database connection.

Enhanced Version: File Persistence & Batch Population

To fix data volatility, this expanded version incorporates binary storage (contacts.dat), an automated seeding function for 100 sample contacts, and a dedicated text export routine (contacts_export.txt).

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

// Define a structure for the contact information
struct Contact {
    char name[50];
    char phone[15];
    char email[50];
};

// Function prototypes
void addContact(struct Contact newContact);
void autoPopulateContacts();
void viewContacts();
void searchContact();
void modifyContact();
void deleteContact();
void loadContacts();
void saveContacts();
void exportContacts();
void displayMenu();

// Global variables
struct Contact contacts[100];
int contactCount = 0;
const char *filename = "contacts.dat";
const char *exportFilename = "contacts_export.txt";

int main() {
    loadContacts(); // Load contacts from file when the program starts
    if (contactCount == 0) {
        autoPopulateContacts(); // Populate with 100 contacts if empty
    }

    int choice;
    while (1) {
        displayMenu();
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        getchar(); // Clear the input buffer

        switch (choice) {
            case 1: {
                struct Contact newContact;
                printf("\nEnter Name: ");
                fgets(newContact.name, sizeof(newContact.name), stdin);
                newContact.name[strcspn(newContact.name, "\n")] = '\0'; // Remove newline
                printf("Enter Phone Number: ");
                fgets(newContact.phone, sizeof(newContact.phone), stdin);
                newContact.phone[strcspn(newContact.phone, "\n")] = '\0'; // Remove newline
                printf("Enter Email: ");
                fgets(newContact.email, sizeof(newContact.email), stdin);
                newContact.email[strcspn(newContact.email, "\n")] = '\0'; // Remove newline

                addContact(newContact);
                break;
            }
            case 2:
                viewContacts();
                break;
            case 3:
                searchContact();
                break;
            case 4:
                modifyContact();
                break;
            case 5:
                deleteContact();
                break;
            case 6:
                exportContacts();
                break;
            case 7:
                saveContacts(); // Save contacts to file before exiting
                printf("\nExiting...\n");
                exit(0);
                break;
            default:
                printf("\nInvalid choice! Please try again.\n");
        }
    }
    return 0;
}

void displayMenu() {
    printf("\n*** Contact Management System ***\n");
    printf("1. Add New Contact\n");
    printf("2. View All Contacts\n");
    printf("3. Search Contact\n");
    printf("4. Modify Contact\n");
    printf("5. Delete Contact\n");
    printf("6. Export Contacts to File\n");
    printf("7. Exit\n");
}

void addContact(struct Contact newContact) {
    if (contactCount < 100) {
        contacts[contactCount++] = newContact;
        printf("\nContact added successfully!\n");
        saveContacts(); // Save the contact immediately
    } else {
        printf("\nContact list is full!\n");
    }
}

void autoPopulateContacts() {
    for (int i = 0; i < 100; i++) {
        struct Contact tempContact;
        snprintf(tempContact.name, sizeof(tempContact.name), "Contact %d", i + 1);
        snprintf(tempContact.phone, sizeof(tempContact.phone), "123456789%d", i % 10);
        snprintf(tempContact.email, sizeof(tempContact.email), "contact%d@example.com", i + 1);
        addContact(tempContact);
    }
    printf("\n100 sample contacts auto-populated.\n");
}

void viewContacts() {
    if (contactCount == 0) {
        printf("\nNo contacts available.\n");
        return;
    }

    printf("\n*** Contact List ***\n");
    for (int i = 0; i < contactCount; i++) {
        printf("Contact %d:\n", i + 1);
        printf("Name: %s\n", contacts[i].name);
        printf("Phone: %s\n", contacts[i].phone);
        printf("Email: %s\n\n", contacts[i].email);
    }
}

void searchContact() {
    char searchName[50];
    printf("\nEnter the name to search: ");
    fgets(searchName, sizeof(searchName), stdin);
    searchName[strcspn(searchName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, searchName) == 0) {
            printf("\nContact 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("\nContact not found.\n");
    }
}

void modifyContact() {
    char modifyName[50];
    printf("\nEnter the name of the contact to modify: ");
    fgets(modifyName, sizeof(modifyName), stdin);
    modifyName[strcspn(modifyName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, modifyName) == 0) {
            printf("\nModifying Contact:\n");
            printf("Enter new name: ");
            fgets(contacts[i].name, sizeof(contacts[i].name), stdin);
            contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0';
            printf("Enter new phone number: ");
            fgets(contacts[i].phone, sizeof(contacts[i].phone), stdin);
            contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0';
            printf("Enter new email: ");
            fgets(contacts[i].email, sizeof(contacts[i].email), stdin);
            contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0';

            printf("\nContact modified successfully!\n");
            saveContacts(); // Save changes to file
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("\nContact not found.\n");
    }
}

void deleteContact() {
    char deleteName[50];
    printf("\nEnter the name of the contact to delete: ");
    fgets(deleteName, sizeof(deleteName), stdin);
    deleteName[strcspn(deleteName, "\n")] = '\0'; // Remove newline

    int found = 0;
    for (int i = 0; i < contactCount; i++) {
        if (strcasecmp(contacts[i].name, deleteName) == 0) {
            for (int j = i; j < contactCount - 1; j++) {
                contacts[j] = contacts[j + 1];
            }
            contactCount--;
            printf("\nContact deleted successfully!\n");
            saveContacts(); // Save changes to file
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("\nContact not found.\n");
    }
}

void loadContacts() {
    FILE *file = fopen(filename, "rb");
    if (file != NULL) {
        fread(&contactCount, sizeof(int), 1, file);
        fread(contacts, sizeof(struct Contact), contactCount, file);
        fclose(file);
    }
}

void saveContacts() {
    FILE *file = fopen(filename, "wb");
    if (file != NULL) {
        fwrite(&contactCount, sizeof(int), 1, file);
        fwrite(contacts, sizeof(struct Contact), contactCount, file);
        fclose(file);
    } else {
        printf("\nError saving contacts!\n");
    }
}

void exportContacts() {
    FILE *file = fopen(exportFilename, "w");
    if (file != NULL) {
        for (int i = 0; i < contactCount; i++) {
            fprintf(file, "Contact %d:\n", i + 1);
            fprintf(file, "Name: %s\n", contacts[i].name);
            fprintf(file, "Phone: %s\n", contacts[i].phone);
            fprintf(file, "Email: %s\n\n", contacts[i].email);
        }
        fclose(file);
        printf("\nContacts exported to %s successfully!\n", exportFilename);
    } else {
        perror("Error exporting contacts");
    }
}

Conclusion

The Contact Management System project is an excellent example of a beginner-friendly application covering fundamental programming concepts such as structures, arrays, string manipulation, and modular programming in C. With an intuitive interface, it provides a functional tool for managing contacts while serving as a reliable foundation for exploring advanced concepts like dynamic memory allocation and database backends.

Post a Comment

Previous Post Next Post