Table of Contents
Introduction – Library Management
Hello Everyone, here we are going to create a Library Management System in C Language. This is console based application which is improve your C Language skills. It allow user to Add books, Display Books, Search Book and Delete Book.
Output – Library Management
Explanation About Project
Here we explain about the Codes of Project Library Management System in parts to understand it brief,
1. Header File
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Here we include necessary header files,
Free hand-Written Notes
2. Define Structure for Book
// Define structure for Book
struct Book {
char title[100];
char author[100];
int pages;
};
Here we define a structure “Book” to represent book information, containing ‘title’, ‘author’, and ‘pages’.
3. Adding a Book
// Function to add a new book
void addBook(struct Book *books, int *numBooks) {
printf("Enter book title: ");
scanf(" %[^\n]", books[*numBooks].title);
printf("Enter author: ");
scanf(" %[^\n]", books[*numBooks].author);
printf("Enter number of pages: ");
scanf("%d", &books[*numBooks].pages);
(*numBooks)++;
printf("Book added successfully.\n");
}
This Function takes an Array of “Book” structure (‘books’) and the number of the existing books (‘numBooks’). It prompts user’s for the title of the book, author of the book, and number of pages of the new book, and then increments the ‘numBooks’ counter and prints a success message.
4. Displaying all Books
// Function to display all books
void displayBooks(struct Book *books, int numBooks) {
printf("\nLibrary Books:\n");
for (int i = 0; i < numBooks; i++) {
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Pages: %d\n", books[i].pages);
printf("\n");
}
}
Here we declare ‘displayBooks’ Function which takes an array of ‘Book’ structures (‘books’) and the number of existing books (‘numBooks’) as arguments. Then, it iterates over each book in the array and prints its title, author, and number of pages.
5. Searching for Books
// Function to search for a book by title
void searchBook(struct Book *books, int numBooks, char *searchTitle) {
int found = 0;
for (int i = 0; i < numBooks; i++) {
if (strcmp(books[i].title, searchTitle) == 0) {
printf("Book found:\n");
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Pages: %d\n", books[i].pages);
found = 1;
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
Here we declare the Function named ‘searchBook’ which takes the array of ‘Book’ structure (‘books’), the number of existing books (‘numBooks’), and the title of the book to search for (‘searchTitle’). It iterates through the array and compares the title of each book with search title using ‘strcmp’. If a match is found, it prints the book details; otherwise, it displays a “Book not found” message.
6. Deleting a Book
// Function to delete a book by title
void deleteBook(struct Book *books, int *numBooks, char *deleteTitle) {
int found = 0;
for (int i = 0; i < *numBooks; i++) {
if (strcmp(books[i].title, deleteTitle) == 0) {
// Shift elements to the left to overwrite the deleted book
for (int j = i; j < *numBooks - 1; j++) {
strcpy(books[j].title, books[j + 1].title);
strcpy(books[j].author, books[j + 1].author);
books[j].pages = books[j + 1].pages;
}
(*numBooks)--;
found = 1;
printf("Book deleted successfully.\n");
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
Here we declare a Function named ‘deleteBook’ which takes an array of ‘Book’ structures (‘books’), the number of existing books (‘numBooks’), and the title of the book to delete (‘deleteTitle’). It searches for the book with the given title, and if found, it shifts the element to overwrite the deleted book and decrements the ‘numBooks’ counter. If the book is not found, it displays a “Book not found” message.
7. Main Function and User Interaction
int main() {
struct Book books[100];
int numBooks = 0;
int choice;
char searchTitle[100];
char deleteTitle[100];
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Display Books\n");
printf("3. Search Book\n");
printf("4. Delete Book\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook(books, &numBooks);
break;
case 2:
displayBooks(books, numBooks);
break;
case 3:
printf("Enter title to search: ");
scanf(" %[^\n]", searchTitle);
searchBook(books, numBooks, searchTitle);
break;
case 4:
printf("Enter title to delete: ");
scanf(" %[^\n]", deleteTitle);
deleteBook(books, &numBooks, deleteTitle);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 5);
return 0;
}
This Main Function initializes necessary variables, such as the array of ‘Book’ structures (‘books’) and the number of existing books (‘numBooks’). It also shows the menu to the user, prompting them to choose an action. Based on the user’s selection, it calls the appropriate functions to add, display, search, or delete a book. The Loop continues until the user chooses to exit the program.
Complete Project Codes
Here is the complete codes of Project Library Management System,
#include <stdio.h> #include <stdlib.h> #include <string.h> // Define structure for Book struct Book { char title[100]; char author[100]; int pages; }; // Function to add a new book void addBook(struct Book *books, int *numBooks) { printf("Enter book title: "); scanf(" %[^\n]", books[*numBooks].title); printf("Enter author: "); scanf(" %[^\n]", books[*numBooks].author); printf("Enter number of pages: "); scanf("%d", &books[*numBooks].pages); (*numBooks)++; printf("Book added successfully.\n"); } // Function to display all books void displayBooks(struct Book *books, int numBooks) { printf("\nLibrary Books:\n"); for (int i = 0; i < numBooks; i++) { printf("Title: %s\n", books[i].title); printf("Author: %s\n", books[i].author); printf("Pages: %d\n", books[i].pages); printf("\n"); } } // Function to search for a book by title void searchBook(struct Book *books, int numBooks, char *searchTitle) { int found = 0; for (int i = 0; i < numBooks; i++) { if (strcmp(books[i].title, searchTitle) == 0) { printf("Book found:\n"); printf("Title: %s\n", books[i].title); printf("Author: %s\n", books[i].author); printf("Pages: %d\n", books[i].pages); found = 1; break; } } if (!found) { printf("Book not found.\n"); } } // Function to delete a book by title void deleteBook(struct Book *books, int *numBooks, char *deleteTitle) { int found = 0; for (int i = 0; i < *numBooks; i++) { if (strcmp(books[i].title, deleteTitle) == 0) { // Shift elements to the left to overwrite the deleted book for (int j = i; j < *numBooks - 1; j++) { strcpy(books[j].title, books[j + 1].title); strcpy(books[j].author, books[j + 1].author); books[j].pages = books[j + 1].pages; } (*numBooks)--; found = 1; printf("Book deleted successfully.\n"); break; } } if (!found) { printf("Book not found.\n"); } } int main() { struct Book books[100]; int numBooks = 0; int choice; char searchTitle[100]; char deleteTitle[100]; do { printf("\nLibrary Management System\n"); printf("1. Add Book\n"); printf("2. Display Books\n"); printf("3. Search Book\n"); printf("4. Delete Book\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: addBook(books, &numBooks); break; case 2: displayBooks(books, numBooks); break; case 3: printf("Enter title to search: "); scanf(" %[^\n]", searchTitle); searchBook(books, numBooks, searchTitle); break; case 4: printf("Enter title to delete: "); scanf(" %[^\n]", deleteTitle); deleteBook(books, &numBooks, deleteTitle); break; case 5: printf("Exiting...\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 5); return 0; }
Conclusion
In this Project Article, we explored how Library Management System is created with C Language. By breaking down the Project Codes in smaller parts we briefly understand how the program flow works.