Mini Project: Implementing a Search Engine with C Plus Plus

in #techclub19 hours ago (edited)
Hello Everyone
I'm AhsanSharif From Pakistan
Greetings to you all, hope you all are well and enjoying a happy moment of life with steem. I'm also good Alhamdulillah.


image.png
Canva Design

I hope everyone is well and enjoying their lives. This is the first challenge of the season in which I participate because I have been busy in the last few days. Now I am participating in the Final Tech Club, a new initiative in which we will see, understand, and learn from different projects. So keeping this in mind, I have also thought of bringing a mini project here using the C++ language.

Search Engine Project Using C++

I have designed this project in such a way that we will work here as a search engine. The basic task in this will be that it will search for a piece of data from the text file, the data that the user will write here as a word.

Here the user will have different types of text files, from which he will input any word, this project will work as a search engine and will find that word in all the files and on which line in which file that text is present.

Overview:

  • First of all, let's talk about the overview of this project. The first thing that comes to mind is the input. The input will be the word or query that the user will enter here.
  • Secondly, search is coming to mind in this project, the function of which is that when the user enters the input, it will search for that word or that query in all text files.
  • Finally, we have the output in which this project will show us on which line or in which text file the text or query that the user has inputted is present and under what name.

Some Key Concepts:

  • First, this program will need to open the file and read it so that it can see what is written in which place.
  • It will then search for that word and match the string to which line in which file the word is present.
  • This program will search for this data or this word among different types of files and then show its results, showing the results for each file separately.
  • This is a basic search engine that we can improve even more, which we can use for large files and a large number of data, and with good speed.

Dev C++

To run our project, first, we need to have a setup in which we can run our code. So, I have DevC++ installed and ready. In this, I will run my project. First, the C++ code will be saved as a SearchEngine file and then some files will be created which will be named .txtand then we will run it and check it.

To run this project of mine, first of all, I opened my DevC++ application and created a project in it which was named Search Engine along with that, I created another file which will be a simple text file in which we will write our text from where we will make the user search for something. In this way, you can create two, three, or four files as many text files as you want and this project will work in it.

image.png

You have to keep in mind that the folder in which you have placed the project you have created should also contain your other text files so that it is easy to search. If you have placed the text file in another folder, then you have to include its proper source in the code so that the data can be searched from the file located there.

As you can see, I have created a folder called ProjectSLC23 and my project and text file are in that folder. As an example, here I have created three files as text files using the extension .txt, and one file is my project file which has the extension .cpp.

image.png

file1.txt:

First of all, let me show you my text file, in which you can see that I have added this random text, which consists of about 14 lines. This is a Lorem Ipsum paragraph taken from the internet, which I am using as a dummy.

image.png

SearchEngine.cpp Explanation:

Include & Namespace


image.png

#include <iostream>: We use it for input and output, just like we use cin for input and cout for output.
#include <fstream>: We use it for file handling, which provides input and output operations.
#include <string>: We use this to handle strings, which invokes the string data type.
using namespace std;: This is used because it avoids prefixing std:: with common things including the string cin and cout.

Function: (SearchInFile)


image.png

This function has two types of arguments: if you write file1.txt here where I have written it if you write filename there, it will search the data from all your files, but I have only one file here, so it will search for this word only from this file. If that file is not opening, it will then show an error message as an output that your file is not opening.

 ifstream file("file1.txt"); For just single file
 ifstream file(filename); For whole files

The second is the query inside it, the query that will be searched within this file.

getline(file, line): Through this, it reads the line of the file in this program.
lineNumber: This is used to keep track of this line number in the program.
line.find(query) != string::npos: If that query is found using it in this program, then the line number of this program and the word and the entire line where that word is written are printed. If we don't find any data there, then it will show an output that your query was not found.file.close()`: When our search is complete, we close this file using this.

Main Function:


image.png

This program prompts the user to enter any word here so that it can be found and then stores it in a query string variable.

Then it calls the function searchInFile that finds the user's given word from this file.

Here I am searching for data from only one file. If you want to search for more files, you can add more calls here. Like this:

searchInFile("file1.txt", query);
searchInFile("file2.txt", query);
searchInFile("file3.txt", query);

Here is full C++ Code:

image.png

image.png

Output:

I searched the Lorem word here is the output:


image.png

I searched for the word Like that was not included in the text file. Here is the output:


image.png

Here is the Video Output:

Thank you so much for staying here. I would like to invite @suboohi, @abdullahw2, and @sergeyk to join this club.

Cc:
@kafio @mohammadfaisal @alejos7ven

Dated: 22-02-2025 About Me
Sort:  
Loading...

Coin Marketplace

STEEM 0.17
TRX 0.24
JST 0.034
BTC 96422.40
ETH 2763.88
SBD 0.67