Skip to content

Commit 7c62d88

Browse files
Create Phone Book using c language
This is my fourth project using c language
1 parent 8e58e5f commit 7c62d88

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Phone Book using c language

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
// Structure to represent a contact
5+
struct Contact {
6+
char name[50];
7+
char phone[15];
8+
};
9+
10+
// Function to add a new contact to the phone book
11+
void addContact(struct Contact phoneBook[], int *numContacts) {
12+
if (*numContacts < 100) {
13+
printf("Enter name: ");
14+
scanf("%s", phoneBook[*numContacts].name);
15+
16+
printf("Enter phone number: ");
17+
scanf("%s", phoneBook[*numContacts].phone);
18+
19+
(*numContacts)++;
20+
printf("Contact added successfully.\n");
21+
} else {
22+
printf("Phone book is full. Cannot add more contacts.\n");
23+
}
24+
}
25+
26+
// Function to display all contacts in the phone book
27+
void displayContacts(struct Contact phoneBook[], int numContacts) {
28+
printf("\nPhone Book:\n");
29+
for (int i = 0; i < numContacts; ++i) {
30+
printf("%d. Name: %s, Phone: %s\n", i + 1, phoneBook[i].name, phoneBook[i].phone);
31+
}
32+
printf("\n");
33+
}
34+
35+
int main() {
36+
struct Contact phoneBook[100];
37+
int numContacts = 0;
38+
int choice;
39+
40+
do {
41+
printf("Phone Book Menu:\n");
42+
printf("1. Add Contact\n");
43+
printf("2. Display Contacts\n");
44+
printf("3. Quit\n");
45+
printf("Enter your choice: ");
46+
scanf("%d", &choice);
47+
48+
switch (choice) {
49+
case 1:
50+
addContact(phoneBook, &numContacts);
51+
break;
52+
case 2:
53+
displayContacts(phoneBook, numContacts);
54+
break;
55+
case 3:
56+
printf("Exiting the phone book.\n");
57+
break;
58+
default:
59+
printf("Invalid choice. Please enter a valid option.\n");
60+
}
61+
62+
} while (choice != 3);
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)