0% found this document useful (0 votes)
3 views3 pages

c Programming

Uploaded by

Madan Kumar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

c Programming

Uploaded by

Madan Kumar R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <string.h>

#define MAX_CONTACTS 100

struct Contact {
char name[50];
char phone[15];
char email[50];
};

struct Contact contacts[MAX_CONTACTS];


int count = 0;

void addContact() {
if (count >= MAX_CONTACTS) {
printf("Contact list full!\n");
return;
}
printf("Enter Name: ");
scanf(" %[^\n]", contacts[count].name);
printf("Enter Phone: ");
scanf(" %[^\n]", contacts[count].phone);
printf("Enter Email: ");
scanf(" %[^\n]", contacts[count].email);
count++;
printf("Contact added successfully.\n");
}

void viewContacts() {
if (count == 0) {
printf("No contacts to display.\n");
return;
}
printf("\n--- Contact List ---\n");
for (int i = 0; i < count; i++) {
printf("Name: %s\nPhone: %s\nEmail: %s\n\n", contacts[i].name,
contacts[i].phone, contacts[i].email);
}
}

void searchContact() {
char searchName[50];
printf("Enter name to search: ");
scanf(" %[^\n]", searchName);
for (int i = 0; i < count; i++) {
if (strcmp(contacts[i].name, searchName) == 0) {
printf("Contact Found:\nName: %s\nPhone: %s\nEmail: %s\n",
contacts[i].name, contacts[i].phone, contacts[i].email);
return;
}
}
printf("Contact not found.\n");
}

void editContact() {
char searchName[50];
printf("Enter name to edit: ");
scanf(" %[^\n]", searchName);
for (int i = 0; i < count; i++) {
if (strcmp(contacts[i].name, searchName) == 0) {
printf("Editing contact for %s\n", contacts[i].name);
printf("Enter new Phone: ");
scanf(" %[^\n]", contacts[i].phone);
printf("Enter new Email: ");
scanf(" %[^\n]", contacts[i].email);
printf("Contact updated.\n");
return;
}
}
printf("Contact not found.\n");
}

void deleteContact() {
char searchName[50];
printf("Enter name to delete: ");
scanf(" %[^\n]", searchName);
for (int i = 0; i < count; i++) {
if (strcmp(contacts[i].name, searchName) == 0) {
for (int j = i; j < count - 1; j++) {
contacts[j] = contacts[j + 1];
}
count--;
printf("Contact deleted.\n");
return;
}
}
printf("Contact not found.\n");
}

void sortContacts() {
struct Contact temp;
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (strcmp(contacts[i].name, contacts[j].name) > 0) {
temp = contacts[i];
contacts[i] = contacts[j];
contacts[j] = temp;
}
}
}
printf("Contacts sorted by name.\n");
}

int main() {
int choice;
do {
printf("\n--- Contact Book Menu ---\n");
printf("1. Add Contact\n2. View Contacts\n3. Search Contact\n4. Edit
Contact\n5. Delete Contact\n6. Sort Contacts\n7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: addContact(); break;
case 2: viewContacts(); break;
case 3: searchContact(); break;
case 4: editContact(); break;
case 5: deleteContact(); break;
case 6: sortContacts(); break;
case 7: printf("Exiting...\n"); break;
default: printf("Invalid choice.\n");
}
} while (choice != 7);

return 0;
}

You might also like