Maximum allowed grace days: Zero Problem Statement: (Phone Book) Give functional
ID: 3703675 • Letter: M
Question
Maximum allowed grace days: Zero Problem Statement: (Phone Book) Give functionality to phone.book.e by adding structures and functions Structures Create two stractures personal info and phone_book in a library hender named libpb.h. The structure phone.book holds an array of personal.info with up to 20 entries (struct personal.info [20]), as well as the number of people in the phone book (int num people). The maximum number of people in this phone book will be 30. The personal.info structure will hold 3 character arrays, the first name (char first [26)), the last name (char last (26]), and the phone number (char phone [15)) Functions 1: Write a funetion to ad a contact (personal.into) to the phone,book uning the prototype below void add.person(struct phone.book pb, struct personal.info persom) The phone.book.e prompts the user to enter the information as seen in the sample out put below. After the new contact information ?s entered, the program wil call the function nd.personand pa" this information to the function. In your function deinition you need to add this new contact to the phone.book. 2: Write a second function using the prototype below to search the phone book for a first name. Read the phone.book.e carefally. The phone.book.e pass this name to the function search t h.pb. In your function definition you need to search for all the contacts that matches the searched first name and print full information of each contact that has the same fist name with the format shown in the sample output void search.pb (atruct phose.book pb, char find.name t 1) Hint: To compare two strings, include the string.h lbrary in your libpb.c and use the function stremp(char *, char .), to linker flags are needed fr this library. stremo() returta 0 (or ?lar) if the eno ar, he sai You can use tatremp) to return true if they are the same. If there is no contact found, print 'No entries with that sane. Sample output for the program: 59Explanation / Answer
// File Name: libpb.h
#ifndef LIBPH_H
#define LIBPH_H
// Defines a structure personal_info to store person information
struct personal_info
{
// Data member to store first name, last name and phone number
char first[26];
char last[26];
char phone[15];
};// End of structure
// Defines a structure to create phone book
struct phone_book
{
// Creates an array of object of structure person_info to store 20 information
struct personal_info person_info[20];
// Counter for number of persons
int num_people;
};// End of structure
// Prototype of function to add a record
void add_person(struct phone_book *pb, struct personal_info person);
// Prototype of function to search a record
void search_pb(struct phone_book pb, char find_name[]);
#endif
---------------------------------------------------
// File Name: libpb.c
#include<string.h>
#include<stdio.h>
#include "libpb.h"
/*
Function to add a record.
struct phone_book *pb -> structure type pointer of phone_book to add a record in phone book
struct personal_info person -> structure object contains person information
*/
void add_person(struct phone_book *pb, struct personal_info person)
{
// Adds person object at num_people index position of phone book person_info array of object
pb->person_info[pb->num_people] = person;
// Increase the counter for number of people
pb->num_people++;
}// End of function
/*
Function to search a record.
struct phone_book pb -> structure type object of phone_book which contains all persons information
char find_name[] -> Contains the name to search
*/
void search_pb(struct phone_book pb, char find_name[])
{
// Loop variable
int c;
char name[50];
// To store found status. Initially zero for not found
int found = 0;
// Loops till number of people in the phone book
for(c = 0; c < pb.num_people; c++)
{
// Compares the parameter name with each person name
if(strcasecmp(find_name, pb.person_info[c].first) == 0)
{
// If found displays first name, last name and phone number
printf(" Name: %s %s", pb.person_info[c].first, pb.person_info[c].last);
printf(" Phone: %s", pb.person_info[c].phone);
// Set the found status to one for found
found = 1;
}// End of if condition
}// End of for loop
// Checks if found value is zero then display message not found
if(found == 0)
printf(" No entries with that name.");
}// End of function
----------------------------------------------------------------
// File Name: PhoneBook.c
#include<stdio.h>
#include<string.h>
#include "libpb.c"
// main function definition
int main()
{
// To store user continue status
char cont;
// To store the name to be searched
char find_name[25];
// Declares an object of phone_book
struct phone_book pb;
// Initializes number of people to zero
pb.num_people = 0;
// Declares an object of personal_info
struct personal_info person;
// Displays add contact message
printf(" ******************************************************************* ");
printf(" Starts with entering new contacts! ");
printf(" ******************************************************************* ");
printf(" Would you like to enter a new contact (Y/N)? ");
// Loops till number of people is less than 20 or cont value is not 'N'
while(pb.num_people < 20)
{
// Accepts user choice to continue or not
scanf("%c", &cont);
// Checks if cont contains 'Y'
if(cont == 'Y')
{
// Accepts first name, last name, and phone number from the user
printf("Enter a first name: ");
scanf("%s", person.first);
printf("Enter %s's last name: ", person.first);
scanf("%s", person.last);
printf("Enter %s's phone number: ", person.first);
scanf("%s", person.phone);
// Calls the function to add the person information in phone book
add_person(&pb, person);
}// End of if condition
// Otherwise checks cont value is 'N'
else if(cont == 'N')
// Then come out of the loop
break;
// Otherwise checks cont value is ' '
else if(cont == ' ')
// Continue the loop
continue;
// Otherwise display error message
else
printf(" Error: User entered '%c'. Must enter either 'Y' or 'N' ", cont);
printf(" Would you like to enter a new name (Y / N)? ");
}// End of while loop
// Search phone book by first name and print person
printf(" ******************************************************************* ");
printf(" Now you can search for names! ");
printf(" ******************************************************************* ");
printf(" Would you like to search for a name (Y / N)? ");
// Loops till user choice is not 'N'
while(1)
{
// Accepts user choice to continue or not
scanf("%c", &cont);
// Checks if cont contains 'Y'
if(cont == 'Y')
{
// Accepts the first name from the user
printf(" Enter a person's name to search for: ");
scanf("%s", find_name);
// Calls the function to search the name in phone book
search_pb(pb, find_name);
}// End of if condition
// Otherwise checks cont value is 'N'
else if(cont == 'N')
// Then come out of the loop
break;
// Otherwise checks cont value is ' '
else if(cont == ' ')
// Continue the loop
continue;
// Otherwise display error message
else
printf(" Error: User entered '%c'. Must enter either 'Y' or 'N' ", cont);
printf(" Would you like to search for a name (Y / N)? ");
}// End of while loop
return 0;
}// End of function
Sample Output:
*******************************************************************
Starts with entering new contacts!
*******************************************************************
Would you like to enter a new contact (Y/N)? Y
Enter a first name: Albert
Enter Albert's last name: Einstein
Enter Albert's phone number: 8467593817
Would you like to enter a new name (Y / N)? s
Error: User entered 's'. Must enter either 'Y' or 'N'
Would you like to enter a new name (Y / N)? Y
Enter a first name: Werner
Enter Werner's last name: Heosemnerg
Enter Werner's phone number: 8674363761
Would you like to enter a new name (Y / N)? Y
Enter a first name: Albert
Enter Albert's last name: Hofmann
Enter Albert's phone number: 7479672695
Would you like to enter a new name (Y / N)? N
*******************************************************************
Now you can search for names!
*******************************************************************
Would you like to search for a name (Y / N)? Y
Enter a person's name to search for: Werner
Name: Werner Heosemnerg
Phone: 8674363761
Would you like to search for a name (Y / N)? Y
Enter a person's name to search for: Ram
No entries with that name.
Would you like to search for a name (Y / N)? Y
Enter a person's name to search for: Albert
Name: Albert Einstein
Phone: 8467593817
Name: Albert Hofmann
Phone: 7479672695
Would you like to search for a name (Y / N)? N