C++ | Searching A Word In A Text File
C++ | Searching A Word In A Text File
Hello peeps! Lord Hypersonic greets you. Today I am here with a new c++ program. This program can search a word user want to search in a given text file and print the positions of the word where it is written (printing line number and position number ).
Want to know how this program can do this magic? Following are the steps to search a word in a text file.
- Declare string variables input_file, wordToFind, line.
- Starting infinite while loop.
- Declaring integer variable line_Number, found.
- Ask for the user to provide the file name.
- Ask for the user to provide the word to search in the text file.
- Declaring ifstream variable file for getting input from the file. And opening the file provided by the user.
- Check if file is true, or in other words checking the availability of the given file. If the file is available then follow step 8 and if file not available then follow step 9.
- If file is true. Then
8.2. Increase the value of line_Number by one (line_Number = line_Number + 1).
8.3. Declaring integer variable position and making it equal to 0.
8.4. Starting for loop from i = starting position of the word in the line to i = size of the line. After each iteration increase i by i+position. The starting position of a word in the given string can be found using find() function from string. It is a member function of the string class, it returns the starting position of the word in the given string and if the word is not in the string then it returns string::npos, means no position. To know more about find() function click here.
8.4.1. Making position equal to position of line (position = line.find(wordToFind,i)).
8.4.2. If position is not equal to string::npos then print the position of the word.
8.4.3. Else break the loop (i.e., if find() function return string::npos then break the loop).
8.5. Close the file (file.close()).
8.6. If found is equal to 0 then word is not found and print word not in the file.
9. If file is not true. Then print file not found.
10. At the end wait for the user to press any key and exit the program.
This is how we can create a program to search a word in a text file. You can use the algorithm given above to make this program in other programming languages.
This is how we can create a program to search a word in a text file. You can use the algorithm given above to make this program in other programming languages.
Note: This program is case sensitive, it will only search the word you enter. Word, word, and WORD are different words for this program.
Source Code:-
/*
Program: Search a word in a text file.
Description: Program that can search word in a text file.
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.com
*/
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
string input_file,wordToFind,line; //declaring string variables input_file, wordToFind and line.
while (1) //starting infinite loop.
{
int line_Number=0,found=0; // declaring and initializing integer variables line_Number and found.
cout<<"\n\nFile: "; getline(cin,input_file); // asking user to provide the file in which user want to search the word.
cout<<"\nWord to find: "; getline(cin,wordToFind); // asking for word which will be searched in the file.
ifstream file(input_file.c_str()); // opening file input stream.
if(file) //checking if file is available.
{
while(getline(file,line)) //reading file line by line.
{
line_Number++; // increasing value of line_Number by 1 (line_Number=line_Number+1)
int position=0; //initializing position with zero.
for(int i=line.find(wordToFind); i<line.length(); i=i+position) // loop to find word in whole line.
{
position=line.find(wordToFind,i); //making position equal to the position where word to find is.
if(position != string::npos) //if word is there in line
{
cout<<endl<<wordToFind<<" is at "<<line_Number<<":"<<position<<endl; // print the position of word.
found=1;//making found equal to 1.
}
else break; //if word not found in line the break the loop.
}
}
file.close(); //closing file.
if(found==0) //if found is zero (i.e., word is not in the file), then
{
cout<<endl<<wordToFind<<" not in file"<<endl; // print word is not in file.
}
}
else //if file provided is not available.
{
cout<<endl<<input_file<<" not found" <<endl; //print file not found.
}
_getch(); //waiting for user to press any key to exit.
}
return 0; //exiting program.
}
If you know another way of making this program then comment it so we can also learn other possible ways of solving this problem.
WORKING:-
WORKING:-
man you are great. Thanks
ReplyDeleteThank you brother :)
DeleteAll your hard work is much appreciated. Nobody can stop to admire you. Lots of appreciation. Word Unscrambler
ReplyDelete