In: Computer Science
Description: The search method should prompt the user for a search phrase. Given the search phrase the search method should search each line in the file for the location of the phrase on each line. If the search phrase is not found on a line then just go to the next line. The search method must find every occurrence of the phrase on a line. The search method should print the line number, the line and the location of the search phrase (“use ^”). After all lines have been searched the search method then prompts the user to enter another search phrase. The search method does not exit until the user enters the phrase EINPUT. See Sample Outputi. Description: The search method should prompt the user for a search phrase. Given the search phrase the search method should search each line in the file for the location of the phrase on each line. If the search phrase is not found on a line then just go to the next line. The search method must find every occurrence of the phrase on a line. The search method should print the line number, the line and the location of the search phrase (“use ^”). After all lines have been searched the search method then prompts the user to enter another search phrase. The search method does not exit until the user enters the phrase EINPUT. See Sample Output
in java please!!!!
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
// main function definition
int main()
{
// To store the word to search
char wordToSearch[256];
ifstream fileRead;
do
{
// Opens the file
fileRead.open("searchPhrase.txt");
// Checks if file is unable to open then displays error
message
if(!fileRead)
{
cout<<"\n File was unable to be opened.\n";
exit(0);
}// End of if condition
// To store the line number
int lineNumber = 0;
// Accepts a word to search
cout<<"Enter the word you want to search for (EINPUT to
stop): ";
cin>>wordToSearch;
// Checks if user entered word is equals to "EINPUT" then
stop
if(strcmp(wordToSearch, "EINPUT") == 0)
break;
cout<<"\n\n";
// Loops till end of the file
while(!fileRead.eof())
{
// To store the word read from file
string currentWord;
// Reads a word from file
fileRead>>currentWord;
// Stores the length of the current word
int len = currentWord.length();
// Checks if current word last character is a '.' then increases
the line number
if(currentWord.at(len-1)=='.')
lineNumber++;
// Checks if current word is equals to user entered word then
displays line number
if(currentWord == wordToSearch)
{
cout<<wordToSearch<<" found on line:
"<<lineNumber + 1<< "\n\n";
break;
}// End of if condition
// Checks if current word is not equals to user entered
word
if(currentWord != wordToSearch)
{
// Checks for end of the file and displays 0 for not found
if(fileRead.eof())
{
cout<<"\n Not found."<<endl;
}// End of if condition
}// End of outer if condition
}// End of while loop
// Close the file
fileRead.close();
}while(1);// End of do - while loop
return 0;
}// End of main function
Sample Output:
Enter the word you want to search for (EINPUT to stop): does
does found on line: 3
Enter the word you want to search for (EINPUT to stop): found
Not found.
Enter the word you want to search for (EINPUT to stop): because
because found on line: 3
Enter the word you want to search for (EINPUT to stop): EINPUT
searchPhrase.txt file contents
For God so loved the world that the gave this one and only Son,
that whoever believes in him shall not perish but have eternal
life. For God did not send his Son into the world to condemn the
world, but to save the world through him.
Whoever believes in him is not condemned but whoever does not
believe stands condemned already because they have not believed in
the name of God's one and only Son.
This is the verdict: Light has come into the world, but people
loved darkness instead of light because their deeds were
evil.
Everyone who does evil hates the light, and will not come into the
light for fear that their deeds will be exposed.
But whoever lives by the truth comes into the light, so that it may
be seen plainly that what they have done has been done in the sight
of God."