in C programming Add additional functionality to your phonebook program from lab
ID: 3594394 • Letter: I
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 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<stdio.h>
#include<stdlib.h>
#include<string.h>
// 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);
}
Explanation / Answer
I have created the required functions to perform the tasks as per your request kindly add these functions to your program and do not forget to add #include time.h and string.h in your header .
void sortPhonebook(struct phonebook **arr)
{
{
int i,j,k;
struct phonebook t;
for(i=1;i {
for(j=0;j {
if(strcmpi(arr[j].fname,arr[i].fname)>0)
{
t=arr[j];
arr[j]=arr[i];
for(k=i;k>j;k--)
arr[k]=arr[k-1];
arr[k+1]=t;
}
}
}
}
}
void searchContact( struct phonebook ** arr)
{
int i;
char f[20], char l[20]
printf("Enter the name for which you want to search phone number")
scanf("%s%s", f ,l)
for ( i=0; i<50;i++)
{
if ( strcmp(arr[i].fname,f)==0 && strcmp(arr[i].lname,f)==0)
printf("contact number %s/n" , arr[i]. phone)
}
}
void randomName(struct phonebook **arr)
{
const int numberOfWords = Size;
for (; ;)
{
srand((unsigned)time(NULL)); //generate a random seed based on time so it's different every time!
int ran = rand()% numberOfWords;
//Generate a random number between 0 to numberOfWords - 1
printf("%s%s", arr.fname[ran],arr.lname[ran]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.