0% found this document useful (0 votes)
4 views3 pages

C Lab Program 14

The document outlines a program that creates an array of structures to store book details and checks for the availability of a specific book based on user input. It includes an algorithm and C code that prompts the user to enter book titles, searches for a specified title, and outputs whether the book is available or not. The program utilizes a structure to hold book information and employs string comparison for the search functionality.

Uploaded by

M.S.Nandhana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

C Lab Program 14

The document outlines a program that creates an array of structures to store book details and checks for the availability of a specific book based on user input. It includes an algorithm and C code that prompts the user to enter book titles, searches for a specified title, and outputs whether the book is available or not. The program utilizes a structure to hold book information and employs string comparison for the search functionality.

Uploaded by

M.S.Nandhana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like