Question

In: Computer Science

in basic c++ program please!! Write a word search and word count program. Assign the following...

in basic c++ program please!!

Write a word search and word count program.

  1. Assign the following text to a string constant.

For God so loved the world that he gave his 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. 21 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.

  1. Prompt the user to enter a word or phrase
  2. Search the string given by the user from the text above and inform how many times the word/phrase was found in the text.

(Hint: You need to use while loop, .find and .length function from string library. Also, you will need to use string::npos.)

  1. Your SOURCE CODE (.cpp file ONLY, such as main.cpp, filename.cpp. It is NOT .sln or any other extensions)
  2. Search for the following (ALL strings are CASE-SENSITIVE!)
    1. God
    2. the world
    3. believe
    4. Jesus
    5. only Son
    6. life

  

Solutions

Expert Solution

C++ code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
//String from which the words will be searched
   string str = "For God so loved the world that he gave his 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. \
   21 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.";

   cout<<"Enter String or phrase to be searched:\n";

   string c;

   //Taking word/phrase from user
   getline(cin,c);

   //Finding the index of word/phrase in the string
size_t found = str.find(c);
int count=0;

//Loop till reaches end of the string
while (found != string::npos){
count++;
  
//Finding word/phrase from next index of the previous occurrence of it.
found = str.find(c, found+1);
}

cout<<"The word/phrase "<<c<< " comes "<<count<<" times."<<"\n";
return 0;
}

Output:


Related Solutions

C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The...
Create a C program that solves the word search puzzle contained in the file 'WordSearchPuzzle.txt'. The words that need to be found are in the file 'WordList.txt'. There are 100 words to be found. You must find the location of the first and last letter of each word as well as the cardinal direction that describes the word's orientation. The location of first and last letters are to be described in terms of row and column, with indexing starting at...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
Please discuss the following question in your own word 300 word count on the following question....
Please discuss the following question in your own word 300 word count on the following question. Continuous process improvement is a significant step in Kotter's last step "make it stick". The last step defines the cultural change for the organization and is marked by employee buy-in, leadership's ability to motivate staff towards change, and redefining the organization's position on the "process change". As a future health care leader you create the vision and point your staff to the path towards...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT