-
Notifications
You must be signed in to change notification settings - Fork 1
/
contactsmanagement.c
160 lines (130 loc) · 3.82 KB
/
contactsmanagement.c
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
#include<stdio.h>
#include<string.h>
#define MAX_CONTACTS 100
#define MAX_NAME_LEN 50
#define MAX_PHONE_LEN 15
#define MAX_EMAIL_LEN 50
struct contact
{
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
char email[MAX_EMAIL_LEN];
};
struct contact contacts[MAX_CONTACTS];
int num_contacts=0;
void add_contact()
{
if(num_contacts==MAX_CONTACTS)
{
printf("Contacts list if full!\n");
return;
}
struct contact new_contact;
printf("\nEnter name: ");
fgets(new_contact.name, MAX_NAME_LEN, stdin);
new_contact.name[strcspn(new_contact.name, "\n")] = '\0'; // remove trailing newline character
printf("Enter phone number: ");
fgets(new_contact.phone, MAX_PHONE_LEN, stdin);
new_contact.phone[strcspn(new_contact.phone, "\n")] = '\0'; // remove trailing newline character
printf("Enter email address: ");
scanf("%s", new_contact.email);
contacts[num_contacts++] = new_contact;
printf("Contact added successfully\n");
}
void list_contacts()
{
printf("\nContacts list:\n");
for (int i = 0; i < num_contacts; i++)
printf("%d. %s, %s, %s\n", i+1, contacts[i].name, contacts[i].phone, contacts[i].email);
}
void delete_contact()
{
int index,i;
printf("\nEnter the index of the contact to delete: ");
scanf("%d",&index);
if(index <1 || index>num_contacts)
{
printf("Invalid Index\n");
return;
}
for(i=index-1;i<num_contacts-1;i++)
{
contacts[i]=contacts[i+1];
}
num_contacts--;
printf("Contact succesfully deleted\n");
}
void edit_contact()
{
char search_name[MAX_NAME_LEN];
int index = -1;
printf("Enter name to edit: ");
scanf("%s", search_name);
for (int i = 0; i < num_contacts; i++)
{
if (strcmp(search_name, contacts[i].name) == 0)
{
index = i;
break;
}
}
if (index == -1)
{
printf("Contact not found\n");
return;
}
printf("Enter new name (leave blank to keep '%s'): ", contacts[index].name);
char new_name[MAX_NAME_LEN];
fgets(new_name, MAX_NAME_LEN, stdin);
new_name[strcspn(new_name, "\n")] = '\0';
if (strlen(new_name) > 0)
strncpy(contacts[index].name, new_name, MAX_NAME_LEN);
getchar();
printf("Enter new phone number (leave blank to keep '%s'): ", contacts[index].phone);
char new_phone[MAX_PHONE_LEN];
fgets(new_phone, MAX_PHONE_LEN, stdin);
new_phone[strcspn(new_phone, "\n")] = '\0';
if (strlen(new_phone) > 0)
strncpy(contacts[index].phone, new_phone, MAX_PHONE_LEN);
getchar();
printf("Enter new email address (leave blank to keep '%s'): ", contacts[index].email);
scanf("%s", contacts[index].email);
printf("Contact edited successfully\n");
}
void main()
{
int choice;
do
{
printf("\nContacts Management\n");
printf("1. Add Contact\n");
printf("2. List Contacts\n");
printf("3. Delete Contact\n");
printf("4. Edit Contact\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
getchar();
switch (choice)
{
case 1:
add_contact();
break;
case 2:
list_contacts();
break;
case 3:
delete_contact();
break;
case 4:
edit_contact();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
break;
}
} while (choice != 5);
}