-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontact.c
More file actions
365 lines (308 loc) · 10.5 KB
/
contact.c
File metadata and controls
365 lines (308 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
#define MAX_NAME 50
#define MAX_PHONE 20
#define MAX_EMAIL 50
#define MAX_ADDRESS 100
#define FILENAME "contacts.txt"
struct Contact {
int id;
char name[MAX_NAME];
char phone[MAX_PHONE];
char email[MAX_EMAIL];
char address[MAX_ADDRESS];
char category[30]; // Family, Work, Friend
};
// Function prototypes
void displayMenu();
int getChoice();
void addContact(struct Contact *contacts, int *count);
void viewContacts(struct Contact *contacts, int count);
void editContact(struct Contact *contacts, int count);
void removeContact(struct Contact *contacts, int *count);
void searchContact(struct Contact *contacts, int count);
void sortContacts(struct Contact *contacts, int count);
void saveToFile(struct Contact *contacts, int count);
int loadFromFile(struct Contact *contacts);
void clearInputBuffer();
void removeNewline(char *str);
void displayContactDetails(struct Contact contact);
int validatePhone(char *phone);
int main() {
struct Contact contacts[MAX_CONTACTS];
int count = 0;
int choice;
// Load existing data
count = loadFromFile(contacts);
printf("\n=== CONTACT BOOK ===\n");
printf("Loaded %d existing contacts.\n", count);
while (1) {
displayMenu();
choice = getChoice();
switch (choice) {
case 1:
addContact(contacts, &count);
break;
case 2:
viewContacts(contacts, count);
break;
case 3:
editContact(contacts, count);
break;
case 4:
removeContact(contacts, &count);
break;
case 5:
searchContact(contacts, count);
break;
case 6:
sortContacts(contacts, count);
break;
case 7:
saveToFile(contacts, count);
printf("\nData saved successfully!\n");
break;
case 8:
saveToFile(contacts, count);
printf("\nGoodbye! Data saved automatically.\n");
return 0;
default:
printf("\nInvalid choice! Please try again.\n");
}
}
}
void displayMenu() {
printf("\n==========================================\n");
printf(" CONTACT BOOK \n");
printf("==========================================\n");
printf("1. Add New Contact\n");
printf("2. View All Contacts\n");
printf("3. Edit Contact\n");
printf("4. Remove Contact\n");
printf("5. Search Contacts\n");
printf("6. Sort Contacts\n");
printf("7. Save Data\n");
printf("8. Exit\n");
printf("==========================================\n");
printf("Enter your choice (1-8): ");
}
int getChoice() {
int choice;
while (scanf("%d", &choice) != 1) {
printf("Invalid input! Please enter a number: ");
clearInputBuffer();
}
clearInputBuffer();
return choice;
}
void addContact(struct Contact *contacts, int *count) {
if (*count >= MAX_CONTACTS) {
printf("\nMaximum contact limit reached!\n");
return;
}
struct Contact newContact;
newContact.id = *count + 1;
printf("\n--- ADD NEW CONTACT ---\n");
printf("Enter name: ");
fgets(newContact.name, MAX_NAME, stdin);
removeNewline(newContact.name);
do {
printf("Enter phone number: ");
fgets(newContact.phone, MAX_PHONE, stdin);
removeNewline(newContact.phone);
} while (!validatePhone(newContact.phone));
printf("Enter email: ");
fgets(newContact.email, MAX_EMAIL, stdin);
removeNewline(newContact.email);
printf("Enter address: ");
fgets(newContact.address, MAX_ADDRESS, stdin);
removeNewline(newContact.address);
printf("Enter category (Family/Work/Friend): ");
fgets(newContact.category, 30, stdin);
removeNewline(newContact.category);
contacts[*count] = newContact;
(*count)++;
printf("\nContact added successfully!\n");
}
void viewContacts(struct Contact *contacts, int count) {
if (count == 0) {
printf("\nNo contacts saved yet.\n");
return;
}
printf("\n==========================================\n");
printf(" ALL CONTACTS \n");
printf("==========================================\n");
printf("ID | Name | Phone | Category\n");
printf("---|--------------------|--------------|-----------\n");
for (int i = 0; i < count; i++) {
printf("%-2d | %-18s | %-12s | %s\n",
contacts[i].id, contacts[i].name, contacts[i].phone, contacts[i].category);
}
printf("==========================================\n");
printf("Total Contacts: %d\n", count);
}
void editContact(struct Contact *contacts, int count) {
if (count == 0) {
printf("\nNo contacts to edit.\n");
return;
}
int id;
printf("\nEnter contact ID to edit: ");
scanf("%d", &id);
clearInputBuffer();
for (int i = 0; i < count; i++) {
if (contacts[i].id == id) {
printf("\n--- EDITING CONTACT ID %d ---\n", id);
displayContactDetails(contacts[i]);
printf("\nEnter new name: ");
fgets(contacts[i].name, MAX_NAME, stdin);
removeNewline(contacts[i].name);
do {
printf("Enter new phone: ");
fgets(contacts[i].phone, MAX_PHONE, stdin);
removeNewline(contacts[i].phone);
} while (!validatePhone(contacts[i].phone));
printf("Enter new email: ");
fgets(contacts[i].email, MAX_EMAIL, stdin);
removeNewline(contacts[i].email);
printf("Enter new address: ");
fgets(contacts[i].address, MAX_ADDRESS, stdin);
removeNewline(contacts[i].address);
printf("Enter new category: ");
fgets(contacts[i].category, 30, stdin);
removeNewline(contacts[i].category);
printf("\nContact updated successfully!\n");
return;
}
}
printf("\nContact ID not found!\n");
}
void removeContact(struct Contact *contacts, int *count) {
if (*count == 0) {
printf("\nNo contacts to remove.\n");
return;
}
int id;
printf("\nEnter contact ID to remove: ");
scanf("%d", &id);
clearInputBuffer();
for (int i = 0; i < *count; i++) {
if (contacts[i].id == id) {
printf("\nRemoving: %s (%s)\n", contacts[i].name, contacts[i].phone);
for (int j = i; j < *count - 1; j++) {
contacts[j] = contacts[j + 1];
}
(*count)--;
printf("\nContact removed successfully!\n");
return;
}
}
printf("\nContact ID not found!\n");
}
void searchContact(struct Contact *contacts, int count) {
if (count == 0) {
printf("\nNo contacts to search.\n");
return;
}
char search[MAX_NAME];
printf("\nEnter name, phone, or category to search: ");
fgets(search, MAX_NAME, stdin);
removeNewline(search);
printf("\n--- SEARCH RESULTS ---\n");
int found = 0;
for (int i = 0; i < count; i++) {
if (strstr(contacts[i].name, search) || strstr(contacts[i].phone, search) ||
strstr(contacts[i].category, search)) {
displayContactDetails(contacts[i]);
printf("\n");
found++;
}
}
if (!found) {
printf("No matching contacts found.\n");
} else {
printf("Found %d matching contact(s).\n", found);
}
}
void sortContacts(struct Contact *contacts, int count) {
if (count == 0) {
printf("\nNo contacts to sort.\n");
return;
}
printf("\nSort by: 1) Name 2) Phone 3) Category\n");
printf("Enter choice: ");
int choice = getChoice();
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
int swap = 0;
switch (choice) {
case 1: // Sort by name
if (strcmp(contacts[j].name, contacts[j + 1].name) > 0) swap = 1;
break;
case 2: // Sort by phone
if (strcmp(contacts[j].phone, contacts[j + 1].phone) > 0) swap = 1;
break;
case 3: // Sort by category
if (strcmp(contacts[j].category, contacts[j + 1].category) > 0) swap = 1;
break;
}
if (swap) {
struct Contact temp = contacts[j];
contacts[j] = contacts[j + 1];
contacts[j + 1] = temp;
}
}
}
printf("\nContacts sorted successfully!\n");
viewContacts(contacts, count);
}
void saveToFile(struct Contact *contacts, int count) {
FILE *file = fopen(FILENAME, "w");
if (!file) {
printf("\nError saving to file!\n");
return;
}
fprintf(file, "%d\n", count);
for (int i = 0; i < count; i++) {
fprintf(file, "%d|%s|%s|%s|%s|%s\n",
contacts[i].id, contacts[i].name, contacts[i].phone,
contacts[i].email, contacts[i].address, contacts[i].category);
}
fclose(file);
}
int loadFromFile(struct Contact *contacts) {
FILE *file = fopen(FILENAME, "r");
if (!file) {
return 0; // No file exists yet
}
int count;
fscanf(file, "%d\n", &count);
for (int i = 0; i < count; i++) {
fscanf(file, "%d|%49[^|]|%19[^|]|%49[^|]|%99[^|]|%29[^\n]\n",
&contacts[i].id, contacts[i].name, contacts[i].phone,
contacts[i].email, contacts[i].address, contacts[i].category);
}
fclose(file);
return count;
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void removeNewline(char *str) {
str[strcspn(str, "\n")] = 0;
}
void displayContactDetails(struct Contact contact) {
printf("ID: %d | Name: %s | Phone: %s | Email: %s | Address: %s | Category: %s",
contact.id, contact.name, contact.phone, contact.email,
contact.address, contact.category);
}
int validatePhone(char *phone) {
if (strlen(phone) < 10) {
printf("Phone number must be at least 10 digits! Try again.\n");
return 0;
}
return 1;
}