Question: in C programming Add additional functionality to your phonebook program from lab# 6. Make it possible for users to: 1) Alphabetically sort the list of
in C programming Add additional functionality to your phonebook program from lab# 6. Make it possible for users to: 1) Alphabetically sort the list of entries by name (first or last). 2) Find a phone number for a given name. 3) Randomly select a friend from the phonebook for you to call. 4) Delete everyone from the phonebook at the same time.
Here is my current code if someone could help me out with a complete code
#include
#include
#include
// create a struct
struct phonebook{
char fname[20];
char lname[20];
char phone[8];
};
void addfriend(struct phonebook **, int *);
void deletefriend(struct phonebook **, int);
void showPhonebook(struct phonebook **);
int main()
{
int i;
// create an array of type struct phonebook
struct phonebook **arr = (struct phonebook **)malloc(50 * sizeof(struct phonebook *));
// index at which new entry is to be added
int index = 0;
// set all entries of arr to NULL
for( i = 0 ; i < 50 ; i++ )
arr[i] = NULL;
while(1)
{
printf("Enter one of the following options ... ");
printf("1) Add friend 2) Delete friend 3) Show phone book 4) Quit ");
int option;
scanf("%d",&option);
switch(option)
{
case 1 : addfriend(arr, &index);
break;
case 2 : deletefriend(arr, index);
break;
case 3 : showPhonebook(arr);
break;
default : exit(0);
}
}
return 0;
}
void addfriend(struct phonebook **arr, int *index)
{
struct phonebook *temp = (struct phonebook *)malloc(sizeof(struct phonebook));
printf("Enter the name of friend ");
scanf("%s%s", temp->fname, temp->lname);
printf("Enter the phone number ");
scanf("%s", temp->phone);
arr[*index] = temp;
(*index)++;
}
void deletefriend(struct phonebook **arr, int index)
{
char f[20], l[20];
printf("Enter the name of friend ");
scanf("%s%s", f, l);
int i;
// find the required contact
for( i = 0 ; i < 50 ; i++ )
// if contact is found
if(arr[i] && !strcmp(arr[i]->fname, f) && !strcmp(arr[i]->lname, l))
// set that element to NULL
arr[i] = NULL;
}
void showPhonebook(struct phonebook **arr)
{
int i;
for( i = 0 ; i < 50 ; i++ )
// if element is not NULL
if(arr[i])
printf("Name : %s %s Phone Number : %s ",arr[i]->fname, arr[i]->lname, arr[i]->phone);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
