PROVIDE A DETAILED /*MULTILINE*/ PSEUDO CODE FOR THE SOURCE CODE BELOW #include
ID: 3757147 • Letter: P
Question
PROVIDE A DETAILED /*MULTILINE*/ PSEUDO CODE FOR THE SOURCE CODE BELOW
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main( )
{
//to read the file
ifstream infile;
string line;//for read line
string word;//for read word
//stores file name
char fileName[20];
cout<<"Enter file name : ";
cin>>fileName;
//get keyword
string searchMe;
cout<<"Enter string to search : ";
cin>>searchMe;
cout<<"Loading the file........"<<endl;
//load the file
infile.open(fileName); //name of file here. plz mention Complete path if file is not at root
//line count
int lineNumber=0;
//word found count
int foundCount=0;
bool found=false;
//if file open
if (infile.is_open()) //if file opened
{
//get lines
while( getline(infile, line,' ') ) { //get row from text file
//increase line count by one
lineNumber++;
//show status
cout<<"Reading line : "<<lineNumber<<endl;
stringstream ss(line); //stream to other variable
found = false;
//scan line by space delimiter
while(getline(ss, word, ' ')){ //row delimiter by space
//if found increase count by one
if(word.compare(searchMe)==0){
foundCount++;
found = true;
}
}
if(found)
cout<<"Found in line : "<<line<<endl;
}
infile.close(); //close file
cout<<"File scan done........"<<endl;
cout<<"Keyword found :"<<foundCount<<" times in the file";
}
else //if file not found show the below message
{
cout << "Sorry, we could not find the file." << endl;
}
return 0;
}
Explanation / Answer
Pseudocode:
start
Input file name from user
Input string to search from user
print Loading the file
Open user entered file for reading
set lineNumber to zero
set foundCount to zero
set found to false
if file open then
while(getline(infile, line,' ')) do
Increment line number
print Reading line number
set found to false
while(getline(ss, word, ' ')) do
if search word found then
Increment found Count
set found to true;
end if
end while
if found true then
print Found in line number
end if
end while
close file
print File scan done
print number of times Keyword found
end if
if file not open then
print Sorry, we could not find the file
end if
end program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.