How to make a Phone Book project In C Programming

 If you guys want to make your career in the programming field then you have to start with programming.


C Programming will clear your all basic concepts of Data Structures, Algorithms, and Programming which will help you to learn other programming languages in the future.


But to learn any programming language, you have to work on projects related to that programming language, which will help you to learn how to use that programming language.


In this post, We have added a mini project in c language to help you clear your programming concept.


Mini Project In C Language


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define MAX_CONTACTS 100


struct Contact {

    char name[50];

    char phoneNumber[15];

    char email[50];

};


struct PhoneBook {

    struct Contact contacts[MAX_CONTACTS];

    int count;

};


void addContact(struct PhoneBook *phoneBook) {

    if (phoneBook->count >= MAX_CONTACTS) {

        printf("Phone book is full.\n");

        return;

    }


    struct Contact newContact;


    printf("Enter name: ");

    fgets(newContact.name, sizeof(newContact.name), stdin);

    newContact.name[strcspn(newContact.name, "\n")] = '\0';


    printf("Enter phone number: ");

    fgets(newContact.phoneNumber, sizeof(newContact.phoneNumber), stdin);

    newContact.phoneNumber[strcspn(newContact.phoneNumber, "\n")] = '\0';


    printf("Enter email (optional): ");

    fgets(newContact.email, sizeof(newContact.email), stdin);

    newContact.email[strcspn(newContact.email, "\n")] = '\0';


    phoneBook->contacts[phoneBook->count++] = newContact;

    printf("Contact added successfully.\n");

}


void removeContact(struct PhoneBook *phoneBook) {

    if (phoneBook->count == 0) {

        printf("Phone book is empty.\n");

        return;

    }


    char searchQuery[50];

    printf("Enter the name or phone number of the contact to remove: ");

    fgets(searchQuery, sizeof(searchQuery), stdin);

    searchQuery[strcspn(searchQuery, "\n")] = '\0';


    int i;

    for (i = 0; i < phoneBook->count; i++) {

        if (strcmp(phoneBook->contacts[i].name, searchQuery) == 0 ||

            strcmp(phoneBook->contacts[i].phoneNumber, searchQuery) == 0) {

            phoneBook->count--;

            phoneBook->contacts[i] = phoneBook->contacts[phoneBook->count];

            printf("Contact removed successfully.\n");

            return;

        }

    }


    printf("Contact not found.\n");

}


void displayContacts(struct PhoneBook *phoneBook) {

    if (phoneBook->count == 0) {

        printf("Phone book is empty.\n");

        return;

    }


    printf("Contacts in the phone book:\n");

    for (int i = 0; i < phoneBook->count; i++) {

        printf("Name: %s\n", phoneBook->contacts[i].name);

        printf("Phone Number: %s\n", phoneBook->contacts[i].phoneNumber);

        printf("Email: %s\n", phoneBook->contacts[i].email);

        printf("\n");

    }

}


int main() {

    struct PhoneBook phoneBook;

    phoneBook.count = 0;


    int choice;

    do {

        printf("Phone Book Menu:\n");

        printf("1. Add Contact\n");

        printf("2. Remove Contact\n");

        printf("3. Display Contacts\n");

        printf("4. Exit\n");

        printf("Enter your choice (1-4): ");

        scanf("%d", &choice);

        getchar();  // consume the newline character from the previous input


        switch (choice) {

            case 1:

                addContact(&phoneBook);

                break;

            case 2:

                removeContact(&phoneBook);

                break;

            case 3:

                displayContacts(&phoneBook);

                break;

            case 4:

                printf("Exiting...\n");

                break;

            default:

                printf("Invalid choice. Please try again.\n");

        }

        printf("\n");

    } while (choice != 4);


    return 0;

}


Now let me explain it:

Step 1:-   define the necessary structures: 'Contact', 'PhoneBook' & 'Contact'

Where the PhoneBook structure holds an array of contacts and keeps a record of number of contacts.


Step2:- defines three functions :- addContact, removeContact, and displayContacts


FunctionPurposeSteps
addContactAdds a new contact 1. Check if the phone book is full. 2. Prompt the user for contact details. 3. Add contact to the phone book. 4. Display a success message.
removeContactRemoves a contact from the phone book.1. Check if the phone book is empty. 2. Prompt the user for the contact to remove. 3. Search for a contact. 4. Remove contact if found. 5. Display success or not-found message.
displayContactsDisplays all contacts in the phone book.1. Check if the phone book is empty. 2. Iterate over contacts and print details. 3. Display the end message

Step 4:- In this program, we add choice in the main function so that the user can choose the action.


Let me show you an output

mini project in c language
mini project in c language


Now you have your phone book.


Conclusion


Here in this post, we add a c project with source code so if you are searching for a mini project in c language then you should check out our post till the end.




No comments:

Post a Comment

Popular Posts