Untitled
unknown
c_cpp
4 years ago
8.7 kB
16
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node Node;
struct node {
char number1[14];
char number2[14];
char name[25];
char city[15];
char email[30];
Node *link;
};
Node *head = NULL;
int strcompare(char s[], char s2[]) {
char str[30], str2[30];
strcpy(str, s);
strcpy(str2, s2);
for(int i = 0; i < strlen(str); i++) {
if(str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] - 'A' + 'a';
}
}
for(int i = 0; i < strlen(str2); i++) {
if(str2[i] >= 'A' && str2[i] <= 'Z') {
str2[i] = str2[i] - 'A' + 'a';
}
}
return strcmp(str, str2);
}
char* strsubcompare(char s[], char s2[]) {
char str[30], str2[30];
strcpy(str, s);
strcpy(str2, s2);
for(int i = 0; i < strlen(str); i++) {
if(str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] - 'A' + 'a';
}
}
for(int i = 0; i < strlen(str2); i++) {
if(str2[i] >= 'A' && str2[i] <= 'Z') {
str2[i] = str2[i] - 'A' + 'a';
}
}
return strstr(str, str2);
}
void newContact() {
Node *newNode = (Node*) malloc(sizeof(Node));
printf("\nEnter the Contact Name: ");
gets(newNode->name);
printf("Enter Mobile Number: ");
gets(newNode->number1);
printf("Enter Email: ");
gets(newNode->email);
printf("Enter City: ");
gets(newNode->city);
printf("\nNew Contact Added Successfully\n\n");
strcpy(newNode->number2, "\0");
if(head == NULL) {
head = newNode;
newNode->link = NULL;
return;
}
Node *temp = head;
Node *prev = NULL;
int cnt = 0;
while(temp != NULL) {
cnt++;
if(strcompare(temp->name, newNode->name) > 0) {
newNode->link = temp;
if(cnt != 1)
prev->link = newNode;
else head = newNode;
return;
}
prev = temp;
temp = temp->link;
}
prev->link = newNode;
newNode->link = temp;
}
void update(Node* temp) {
while(1) {
int sz = strlen(temp->number2);
printf("1. Update Name\n");
printf("2. Update Email\n");
printf("3. Update City\n");
printf("4. Update Number-1\n");
if(sz>0) printf("5. Update Number-2\n");
printf("b. Enter 'b' to back to the previous menu\n");
char item;
printf("Enter your choice: ");
scanf("%c", &item);
getchar();
int num = item - '0';
printf("%c %d\n\n", item, num);
switch(num) {
case 1:
printf("Enter the name: ");
gets(temp->name);
printf("Name Updated Successfully\n");
break;
case 2:
printf("Enter the Email: ");
gets(temp->email);
printf("Email Updated Successfully\n");
break;
case 3:
printf("Enter the city: ");
gets(temp->city);
printf("City Updated Successfully\n");
break;
case 4:
printf("Enter the Number-1: ");
gets(temp->number1);
printf("Number-1 Updated Successfully\n");
break;
case 5:
printf("Enter the Number-1: ");
gets(temp->number1);
printf("Number-1 Updated Successfully\n");
break;
case 50:
return;
default:
printf("Invalid Choice\n");
}
}
}
void details(int num) {
Node *temp = head;
int cnt = 0;
while(temp != NULL) {
++cnt;
if(cnt == num) {
while(1) {
if(strlen(temp->number2) != 0)
printf("\nName: %s\nMobile-1: %s\nMobile-2: %s\nEmail: %s\nCity: %s\n\n", temp->name, temp->number1, temp->number2, temp->email, temp->city);
else
printf("\nName: %s\nMobile-1: %s\nEmail: %s\nCity: %s\n\n", temp->name, temp->number1, temp->email, temp->city);
int n;
printf("\n1. Update This Contact\n2. Add a New Number to the Contact\n3. Back to the list\n\n");
printf("Enter your Choice: ");
scanf("%d", &n);
getchar();
switch(n) {
case 1:
update(temp);
break;
case 2:
if(strlen(temp->number2) > 0) {
printf("\nMaximum Limit to add new number is reached!!!\n");
break;
}
printf("Enter the new Number: ");
scanf("%s", temp->number2);
printf("\nNumber Added to the Contact Successfully\n\n");
break;
case 3:
return;
default:
printf("Invalid Choice\n");
}
}
return;
}
temp = temp->link;
}
}
void seeList(int breaker) {
printf("\n");
Node *temp = head;
if(temp == NULL) {
printf("\nContact List is Empty!!!\n\n");
return;
}
while(1) {
temp = head;
int i = 0;
printf("Contact List\n");
printf("-------------\n");
while(temp != NULL) {
printf("%d . %s\n\n", ++i, temp->name);
temp = temp->link;
}
if(breaker == 1) return;
printf("\nEnter the corresponding number to See Details or b to back to the main menu: ");
int num;
char ch;
scanf(" %c", &ch);
num = ch - '0';
if(ch == 'b') return;
else if(num > i || num < 0) {
printf("Invalid Choice\n");
continue;
}
else {
details(num);
}
}
}
void deleteContact() {
while(1) {
seeList(1);
if(head == NULL) {
printf("Nothing to Delete!!!\n");
return;
}
printf("Enter the Corresponding Number to delete or b to back to menu: ");
int cnt = 0;
char item;
scanf(" %c", &item);
int num = item - '0';
if(num == 50) return;
if(num == 1) {
head = head->link;
printf("\nContact Successfully Deleted\n\n");
if(head == NULL) return;
continue;
}
Node *temp = head;
Node *prev = NULL;
while(temp != NULL) {
if(++cnt == num) {
prev->link = temp->link;
printf("\nContact Successfully Deleted\n\n");
}
prev = temp;
temp = temp->link;
}
}
}
void searchByName() {
getchar();
printf("\nEnter the name of the Contact: ");
char nm[30];
gets(nm);
while(1) {
Node *temp = head;
int cnt[100];
int tmp = 0, tmp2 = 0;
printf("\nSearch Results\n");
printf("---------------\n");
while(temp != NULL) {
++tmp;
char* ptr = strsubcompare(temp->name, nm);
if(ptr != NULL) {
++tmp2;
printf("%d. %s\n", tmp2, temp->name);
cnt[tmp2] = tmp;
}
temp = temp->link;
}
printf("\nEnter the Corresponding Number to see details or b to back to menu: ");
char item;
scanf(" %c", &item);
int num = item - '0';
if(num == 50) return;
details(cnt[num]);
}
}
int main() {
int n;
while(1) {
printf("\t -------Menu-------\n");
printf("\t*********************\n");
printf("\t1. Add New Contact\n");
printf("\t2. See Contact List\n");
printf("\t3. Search Contact by Name\n");
printf("\t4. Delete Contact\n");
printf("\t5. Exit PhoneBook\n");
printf("Enter the desired number: ");
scanf("%d", &n);
switch(n) {
case 1:
getchar();
newContact();
break;
case 2:
seeList(0);
break;
case 3:
searchByName();
break;
case 4:
deleteContact();
break;
case 5:
printf("Phonebook exited\n");
return 0;
default:
printf("\nInvalid Choice!!!\n");
}
}
return 0;
}
Editor is loading...