Question

In: Computer Science

Description: The search method should prompt the user for a search phrase. Given the search phrase...

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!!!!

Solutions

Expert Solution

#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."


Related Solutions

Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome...
provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome or no.
: Find the inverse of the three matrices listed below. The code should prompt the user...
: Find the inverse of the three matrices listed below. The code should prompt the user to input the size of the matrix and to put the value of each element in the matrix. The output should contain the solution to the inverse from using a function created by you, and the solution found using the NumPy package. I1 = [ 1 2 3 4]−1 I2 = [ 1 2 3 4 5 6 7 2 9 ] −1 I3...
create a program that will verify a user's login credentials. The program should prompt the user...
create a program that will verify a user's login credentials. The program should prompt the user to enter his/her username and password at the keyboard. Then it should read the data from a data file named "login.txt". The file "login.txt" will contain a list of 3 valid usernames and passwords to verify the login information supplied by a user.  If the username and password entered by the user matches one of the sets read from the file, the program should print...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
For a given password system, calculate the search space for a general user and a lazy...
For a given password system, calculate the search space for a general user and a lazy user. Suppose only 11 special characters can be input. Program in java.   Input (by the evaluator): 1st line: number of characters 2nd line: numbers allowed [Y/N]? must be included [Y/N]? 3rd line: letters allowed [Y/N]? case sensitive [Y/N]? must be included [Y/N]? 4th line: special character allowed [Y/N]? must be included [Y/N}
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user...
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user to take the number of circles and their radius values. Then it should compute the areas of each circle. Finally, your algorithm will print both radius and area of all circles into the output. [N.B. you need to use iterative statement for solving the problem. Consider Pi = 3.14159] Input: Area of how many Circles you want to compute? 3 Key in radius values:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT