Phonebook Management System in C Micro Project
Phonebook Management System in C
Welcome to this tutorial on building a phonebook management system using the C programming language. In this project, we will create a program that allows users to add contacts, search for contacts, and display all contacts in the phonebook. Let's get started!
The Code
#include
#include
#include
#define MAX_CONTACTS 100
struct Contact {
char name[100];
char phone[15];
};
struct Contact contacts[MAX_CONTACTS];
int contactCount = 0;
void addContact() {
if (contactCount >= MAX_CONTACTS) {
printf("Phonebook is full. Cannot add more contacts.\n");
return;
}
struct Contact newContact;
printf("Enter Name: ");
scanf(" %[^\n]s", newContact.name);
printf("Enter Phone Number: ");
scanf(" %[^\n]s", newContact.phone);
contacts[contactCount++] = newContact;
printf("Contact added successfully.\n");
}
void searchContact() {
char searchName[100];
printf("Enter Name to search: ");
scanf(" %[^\n]s", searchName);
int found = 0;
for (int i = 0; i < contactCount; 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);
found = 1;
break;
}
}
if (!found) {
printf("Contact not found.\n");
}
}
void displayContacts() {
if (contactCount == 0) {
printf("Phonebook is empty.\n");
return;
}
printf("Contacts in the Phonebook:\n");
for (int i = 0; i < contactCount; i++) {
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
printf("---------------------------\n");
}
}
int main() {
int choice;
while (1) {
printf("\n----- Phonebook Management System -----\n");
printf("1. Add Contact\n");
printf("2. Search Contact\n");
printf("3. Display Contacts\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addContact();
break;
case 2:
searchContact();
break;
case 3:
displayContacts();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 1
Enter Name: John Doe
Enter Phone Number: 1234567890
Contact added successfully.
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 1
Enter Name: Jane Smith
Enter Phone Number: 9876543210
Contact added successfully.
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 2
Enter Name to search: John Doe
Contact found:
Name: John Doe
Phone: 1234567890
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 2
Enter Name to search: Alice Brown
Contact not found.
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 3
Contacts in the Phonebook:
Name: John Doe
Phone: 1234567890
---------------------------
Name: Jane Smith
Phone: 9876543210
---------------------------
----- Phonebook Management System -----
1. Add Contact
2. Search Contact
3. Display Contacts
4. Exit
Enter your choice: 4
Exiting...
Running the Program
- Open a C compiler or IDE of your choice.
- Create a new project and add the provided code.
- Build and run the program.
- You can now interact with the phonebook management system through the console interface.
Conclusion
Congratulations! You have successfully implemented a phonebook management system in C. This project provides a solid foundation for managing contacts and can be further expanded with additional features like contact deletion, modification, sorting, or saving contacts to a file for persistent storage.
I hope you found this tutorial helpful. Happy coding!
Comments
Post a Comment