mirror of
https://github.com/Sosokker/C-Practice.git
synced 2025-12-19 18:34:05 +01:00
Add practice project
This commit is contained in:
commit
9549efc60b
132
contact.c
Normal file
132
contact.c
Normal file
@ -0,0 +1,132 @@
|
||||
// File : contact.c
|
||||
// Description : Implement functions defined in contact.h
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "contact.h"
|
||||
|
||||
void addContact(Contact contacts[], int *numContacts) {
|
||||
if (*numContacts >= MAX_CONTACTS) {
|
||||
printf("Contact list is full.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Contact newContact;
|
||||
printf("Enter name: ");
|
||||
fgets(newContact.name, MAX_NAME_LENGTH, stdin);
|
||||
newContact.name[strcspn(newContact.name, "\n")] = '\0';
|
||||
|
||||
printf("Enter phone number: ");
|
||||
fgets(newContact.phone, MAX_PHONE_LENGTH, stdin);
|
||||
newContact.phone[strcspn(newContact.phone, "\n")] = '\0';
|
||||
|
||||
printf("Enter email address: ");
|
||||
fgets(newContact.email, MAX_EMAIL_LENGTH, stdin);
|
||||
newContact.email[strcspn(newContact.email, "\n")] = '\0';
|
||||
|
||||
contacts[*numContacts] = newContact;
|
||||
(*numContacts)++;
|
||||
printf("Contact added successfully.\n");
|
||||
}
|
||||
|
||||
void displayContacts(Contact contacts[], int numContacts) {
|
||||
if (numContacts == 0) {
|
||||
printf("Contact list is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Contacts:\n");
|
||||
for (int i = 0; i < numContacts; i++) {
|
||||
printf("Name: %s\n", contacts[i].name);
|
||||
printf("Phone: %s\n", contacts[i].phone);
|
||||
printf("Email: %s\n", contacts[i].email);
|
||||
printf("--------------------\n");
|
||||
}
|
||||
}
|
||||
|
||||
void searchContacts(Contact contacts[], int numContacts, char *searchTerm) {
|
||||
if (numContacts == 0) {
|
||||
printf("Contact list is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Search results for '%s':\n", searchTerm);
|
||||
int found = 0;
|
||||
for (int i = 0; i < numContacts; i++) {
|
||||
if (strstr(contacts[i].name, searchTerm) != NULL ||
|
||||
strstr(contacts[i].phone, searchTerm) != NULL ||
|
||||
strstr(contacts[i].email, searchTerm) != NULL) {
|
||||
printf("Name: %s\n", contacts[i].name);
|
||||
printf("Phone: %s\n", contacts[i].phone);
|
||||
printf("Email: %s\n", contacts[i].email);
|
||||
printf("--------------------\n");
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No matching contacts found.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void updateContact(Contact contacts[], int numContacts, char *searchTerm) {
|
||||
if (numContacts == 0) {
|
||||
printf("Contact list is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int found = 0;
|
||||
for (int i = 0; i < numContacts; i++) {
|
||||
if (strstr(contacts[i].name, searchTerm) != NULL ||
|
||||
strstr(contacts[i].phone, searchTerm) != NULL ||
|
||||
strstr(contacts[i].email, searchTerm) != NULL) {
|
||||
printf("Contact found. Enter new details:\n");
|
||||
|
||||
printf("Enter name: ");
|
||||
fgets(contacts[i].name, MAX_NAME_LENGTH, stdin);
|
||||
contacts[i].name[strcspn(contacts[i].name, "\n")] = '\0';
|
||||
|
||||
printf("Enter phone number: ");
|
||||
fgets(contacts[i].phone, MAX_PHONE_LENGTH, stdin);
|
||||
contacts[i].phone[strcspn(contacts[i].phone, "\n")] = '\0';
|
||||
|
||||
printf("Enter email address: ");
|
||||
fgets(contacts[i].email, MAX_EMAIL_LENGTH, stdin);
|
||||
contacts[i].email[strcspn(contacts[i].email, "\n")] = '\0';
|
||||
|
||||
printf("Contact updated successfully.\n");
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No matching contacts found.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void deleteContact(Contact contacts[], int *numContacts, char *searchTerm) {
|
||||
if (numContacts == 0) {
|
||||
printf("Contact list is empty.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int found = 0;
|
||||
for (int i = 0; i < *numContacts; i++) {
|
||||
if (strstr(contacts[i].name, searchTerm) != NULL ||
|
||||
strstr(contacts[i].phone, searchTerm) != NULL ||
|
||||
strstr(contacts[i].email, searchTerm) != NULL) {
|
||||
for (int j = i; j < *numContacts - 1; j++) {
|
||||
contacts[j] = contacts[j + 1];
|
||||
}
|
||||
(*numContacts)--;
|
||||
printf("Contact deleted successfully.\n");
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No matching contacts found.\n");
|
||||
}
|
||||
}
|
||||
21
contact.h
Normal file
21
contact.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef CONTACT_H
|
||||
#define CONTACT_H
|
||||
|
||||
#define MAX_NAME_LENGTH 50
|
||||
#define MAX_PHONE_LENGTH 15
|
||||
#define MAX_EMAIL_LENGTH 50
|
||||
#define MAX_CONTACTS 100
|
||||
|
||||
typedef struct {
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char phone[MAX_PHONE_LENGTH];
|
||||
char email[MAX_EMAIL_LENGTH];
|
||||
} Contact;
|
||||
|
||||
void addContact(Contact contacts[], int *numContacts);
|
||||
void displayContacts(Contact contacts[], int numContacts);
|
||||
void searchContacts(Contact contacts[], int numContacts, char *searchTerm);
|
||||
void updateContact(Contact contacts[], int numContacts, char *searchTerm);
|
||||
void deleteContact(Contact contacts[], int *numContacts, char *searchTerm);
|
||||
|
||||
#endif
|
||||
BIN
contact_management_system.exe
Normal file
BIN
contact_management_system.exe
Normal file
Binary file not shown.
54
main.c
Normal file
54
main.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "contact.h"
|
||||
#include "utils.h"
|
||||
|
||||
int main() {
|
||||
Contact contacts[MAX_CONTACTS];
|
||||
int numContacts = 0;
|
||||
int choice;
|
||||
char searchTerm[MAX_NAME_LENGTH];
|
||||
|
||||
while (1) {
|
||||
printMenu();
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
clearInputBuffer();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
addContact(contacts, &numContacts);
|
||||
break;
|
||||
case 2:
|
||||
displayContacts(contacts, numContacts);
|
||||
break;
|
||||
case 3:
|
||||
printf("Enter search term: ");
|
||||
fgets(searchTerm, MAX_NAME_LENGTH, stdin);
|
||||
searchTerm[strcspn(searchTerm, "\n")] = '\0';
|
||||
searchContacts(contacts, numContacts, searchTerm);
|
||||
break;
|
||||
case 4:
|
||||
printf("Enter search term: ");
|
||||
fgets(searchTerm, MAX_NAME_LENGTH, stdin);
|
||||
searchTerm[strcspn(searchTerm, "\n")] = '\0';
|
||||
updateContact(contacts, numContacts, searchTerm);
|
||||
break;
|
||||
case 5:
|
||||
printf("Enter search term: ");
|
||||
fgets(searchTerm, MAX_NAME_LENGTH, stdin);
|
||||
searchTerm[strcspn(searchTerm, "\n")] = '\0';
|
||||
deleteContact(contacts, &numContacts, searchTerm);
|
||||
break;
|
||||
case 6:
|
||||
printf("Exiting program.\n");
|
||||
exit(0);
|
||||
default:
|
||||
printf("Invalid choice. Please try again.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
utils.c
Normal file
17
utils.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include "utils.h"
|
||||
|
||||
void clearInputBuffer() {
|
||||
int c;
|
||||
while ((c = getchar()) != '\n' && c != EOF) {}
|
||||
}
|
||||
|
||||
void printMenu() {
|
||||
printf("\nContact Management System\n");
|
||||
printf("1. Add contact\n");
|
||||
printf("2. Display all contacts\n");
|
||||
printf("3. Search contact\n");
|
||||
printf("4. Update contact\n");
|
||||
printf("5. Delete contact\n");
|
||||
printf("6. Exit\n");
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user