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...
Write a binary search algorithm in C language by performing following steps: Prompt the user to...
Write a binary search algorithm in C language by performing following steps: Prompt the user to enter the number of array elements (say, N).   Read the N different values (define these values to be of type integer).   Read the element (integer value) which you want to search. Invoke a function to display the values in the array. The function should take the array reference and number of elements as arguments and should have the return type as void. The function...
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...
def main(): phrase = input('Enter a phrase: ') # take a phrase from user acronym =...
def main(): phrase = input('Enter a phrase: ') # take a phrase from user acronym = '' # initialize empty acronym for word in phrase.split(): # for each word in phrase acronym += word[0].upper() # add uppercase version of first letter to acronym print('Acronym for ' + phrase + ' is ' + acronym) # print results main() The program should then open the input file, treat whatever is on each line as a phrase and make its acronym using...
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
C++ code a program should prompt the user for the name of the output file. The...
C++ code a program should prompt the user for the name of the output file. The file should be opened for write operations. Values calculated by the program will be written to this file. The program must verify that the file opened correctly. If the file did not open, an error message should be printed and the user should be prompted again. This should continue until the user supplies a valid filename.
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.
Description Your program must start and keep dialog with the user. Please create the first prompt...
Description Your program must start and keep dialog with the user. Please create the first prompt for the dialog. After that your program must accept the user’s response and echo it (output to screen) in upper case and adding the question mark at the end. The program must run endlessly until the user say “stop” in any combination of cases. Then you program must say “GOODBYE!” and quit. Example: HELLO, I AM THE PROGRAM Hi, I am Michael HI, I...
: 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT