Program 14:
Develop a program to create an array of structures to store book details and check whether a
specific book, as requested by the user, is available or not.
Algorithm:
1. Start
2. Define a structure Book with:
title
author (though author is not used in this program)
3. Declare:
Array library[3] of type Book
String search
Integer found = 0
4. Input book titles:
For i = 0 to 2:
Read library[i].title
5. Input the title to search → search
6. Search for the book:
For i = 0 to 2:
Compare library[i].title with search using string comparison
If both are equal:
Print "Book is available"
Set found = 1
Exit loop
7. Check result:
If found == 0:
Print "Book not found"
8. Stop
Code:
#include <stdio.h>
#include <string.h>
struct Book
{
char title[50];
char author[50];
};
int main()
{
struct Book library[3];
char search[50];
int found = 0;
for (int i = 0; i < 3; i++)
{
printf("Enter title for book %d: ", i+1);
scanf("%s", library[i].title);
}
printf("\nEnter book title to search: ");
scanf("%s", search);
for (int i = 0; i < 3; i++)
{
if (strcmp(library[i].title, search) == 0)
{
printf("Book '%s' is available.\n", search);
found = 1;
break;
}
}
if (!found)
printf("Book not found.\n");
return 0;
}
Output:
Enter title for book 1: Physics
Enter title for book 2: Chemistry
Enter title for book 3: Math
Enter book title to search: Math
Book 'Math' is available.
Enter title for book 1: Physics
Enter title for book 2: Chemistry
Enter title for book 3: Math
Enter book title to search: Biology
Book not found.