Begin implementing the search_contact() module without changing any previously completed code. Add a permanent search-mode framework that asks the user to choose between searching by ID or searching by Name. Choice 1 represents ID search and choice 2 represents Name search. For this task, the two branches only print the selected mode and do not perform an actual search yet. An invalid mode prints Invalid Search Mode. This framework remains in place for all later search tasks.
A sequence of menu choices. Menu choice 3 opens the search module, followed by a search mode of 1 or 2. The application finally receives menu choice 0.
After Search is selected, print Search Mode: ID for mode 1, Search Mode: Name for mode 2, or Invalid Search Mode for any other value.
Search mode 1 represents ID search. Search mode 2 represents Name search. Any other integer is invalid. The existing Add, Display, and main menu functionality must remain unchanged.
3 1 0
Contact Book Ready.
Phonebook Capacity: 100
Contacts: 0
Menu:
Search
Search Mode: ID
Menu:
Exit
A permanent search-mode decision is established before implementing the actual algorithms. This lets later tasks add ID and name searching as separate branches without restructuring the search function.
#include <stdio.h>
#include <string.h>
struct Contact {
int id;
char name[50];
char phone[15];
};
struct Contact phonebook[100];
int contact_count = 0;
void add_contact();
void display_contacts();
void search_contact();
void update_contact();
void delete_contact();
void add_contact() {
if (contact_count >= 100) {
printf("\nPhonebook Full");
return;
}
int id;
if (scanf("%d", &id) == 1) {
printf("\nID: %d", id);
if (id > 0) {
printf("\nValid ID");
{
int duplicate = 0;
for (int i = 0; i < contact_count; i++) {
if (phonebook[i].id == id) {
duplicate = 1;
break;
}
}
if (duplicate) {
printf("\nDuplicate ID");
return;
}
}
getchar();
if (fgets(phonebook[contact_count].name,
sizeof(phonebook[contact_count].name), stdin) != NULL) {
phonebook[contact_count].name[
strcspn(phonebook[contact_count].name, "\n")
] = '\0';
printf("\nName: %s", phonebook[contact_count].name);
{
char phone_input[100];
if (fgets(phone_input, sizeof(phone_input), stdin) != NULL) {
phone_input[
strcspn(phone_input, "\n")
] = '\0';
printf("\nPhone: %s", phone_input);
if (strlen(phone_input) >= 1 &&
strlen(phone_input) <= 14) {
strcpy(phonebook[contact_count].phone, phone_input);
printf("\nValid Phone");
phonebook[contact_count].id = id;
contact_count++;
printf("\nContact Added");
} else {
printf("\nInvalid Phone");
}
}
}
}
} else {
printf("\nInvalid ID");
}
}
}
void display_contacts() {
if (contact_count == 0) {
printf("\nPhonebook Empty");
return;
}
printf("\nID Name Phone");
for (int i = 0; i < contact_count; i++) {
printf("\n%d %s %s",
phonebook[i].id,
phonebook[i].name,
phonebook[i].phone);
}
}
void search_contact() {
// Write your code here
}
void update_contact() {
}
void delete_contact() {
}
int main() {
int choice;
printf("Contact Book Ready.");
printf("\nPhonebook Capacity: %d", 100);
printf("\nContacts: %d", contact_count);
while (1) {
printf("\nMenu:");
if (scanf("%d", &choice) != 1) {
break;
}
switch (choice) {
case 1:
printf("\nAdd");
add_contact();
break;
case 2:
printf("\nDisplay");
display_contacts();
break;
case 3:
printf("\nSearch");
search_contact();
break;
case 4:
printf("\nUpdate");
update_contact();
break;
case 5:
printf("\nDelete");
delete_contact();
break;
case 0:
printf("\nExit");
return 0;
default:
printf("\nInvalid Choice");
break;
}
}
return 0;
}Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used