Write a word search and word count program Program Used Visu
Write a word search and word count program.
Program Used: Visual Studios, C++
Use string, for loop or while loop, and substr if possible.
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.
2) Prompt the user to enter a word or phrase
3) 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.)
2) Search for the following (ALL strings are CASE-SENSITIVE!)
a. God
b. the world
c. believe
d. Jesus
e. only Son
f. life
Solution
// C++ code to find count of word/phrase in string
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
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.\";
string phrase;
cout << \"Enter word/phrase: \";
getline(cin, phrase);
int size = str.size();
int i = 0;
int count = 0;
// find first position
size_t position = str.find(phrase, 0);
while(position != string::npos)
{
count++;
position = str.find(phrase,position+1);
}
if(count > 0)
cout << phrase << \" occured \" << count << \" times\ \" << endl;
else
cout << phrase << \" is not present in given string\ \";
return 0;
}
/*
output:
Enter word/phrase: God
God occured 4 times
Enter word/phrase: the world
the world occured 5 times
Enter word/phrase: believe
believe occured 4 times
Enter word/phrase: Jesus
Jesus is not present in given string
Enter word/phrase: only Son
only Son occured 2 times
Enter word/phrase: life
life occured 1 times
*/

