Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <stdio.

h>
#include <string.h>

#define MAX_BOOKS 100

// Write code for Book Structure


struct Book {
char title[100];
char author[100];
char isbn[20];
int year;
};

// Function declarations
struct Book initBook(char title[], char author[], char isbn[], int year);
void printBook(struct Book b);
int findBookByISBN(struct Book books[], int count, char isbn[]);
int countBooksAfterYear(struct Book books[], int count, int year);

int main() {
struct Book books[MAX_BOOKS];
int count = 0;

books[count++] = initBook("Noli Me Tangere", "Jose Rizal", "9780143039693",


1887);
books[count++] = initBook("El Filibusterismo", "Jose Rizal", "9780143039525",
1891);
books[count++] = initBook("Florante at Laura", "Francisco Balagtas",
"9789715507719", 1838);

// Print all books


for (int i = 0; i < count; i++) {
printBook(books[i]);
}

// Search for a book by ISBN


char searchISBN[] = "9780143039525";
int foundIndex = findBookByISBN(books, count, searchISBN);
if (foundIndex != -1) {
printf("Found book with ISBN %s:\n", searchISBN);
printBook(books[foundIndex]);
} else {
printf("Book with ISBN %s not found.\n", searchISBN);
}
// Count books published after a certain year
int year = 1890;
int booksAfterYear = countBooksAfterYear(books, count, year);
printf("Number of books published after %d: %d\n", year, booksAfterYear);

return 0;
}

// Write code for Functions


struct Book initBook(char title[], char author[], char isbn[], int year){
struct Book b;

strcpy(b.title, title);
strcpy(b.author, author);
strcpy(b.isbn, isbn);
b.year = year;

return b;
}

void printBook(struct Book b){


printf("Title: %s\n", b.title);
printf("Author: %s\n", b.author);
printf("ISBN: %s\n", b.isbn);
printf("Year: %d\n", b.year);
printf("\n");
}

int findBookByISBN(struct Book books[], int count, char isbn[]) {


for (int i = 0; i < count; i++) {
if (strcmp(books[i].isbn, isbn) == 0) {
return i;
}
}
return -1;
}

int countBooksAfterYear(struct Book books[], int count, int year) {


int countAfterYear = 0;
for (int i = 0; i < count; i++) {
if (books[i].year > year) {
countAfterYear++;
}
}
return countAfterYear; }

You might also like