I am needing to write a word search program that will take the user's input and let them know how many times the word or phrase input is found in the passage below (assigned to "john3"). I am having great difficulty with my while loop, and am not quite sure how to set the parameters. Additionally, how do I use the find function to output number of times found rather than the indexes at which the user input is found? Here are the explicit directions for this program:
"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.
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.)"
Here is the code I have written so far:______________________________________
#include <iostream>
#include <string>
using namespace std;
int main() {
string john3 = "For God so loved the world that he gave his one and only Son."
string userInput;
cout << "Enter a word or phrase: ";
getline(cin, userInput);
while () {
if (string::npos == john3.find(userInput)) {
cout << john3.find(userInput) << endl;
userInput++;
}
else {
cout << "Unfortunately, your word or phrase was not found within John 3:16-21." << endl;
}
}
cout << "The word or phrase '" << userInput << "' has been found " << " times in John 3:16-21." << endl;
#include <iostream>
#include <string>
int main()
{
// 1) Assign the following text to a string constant.
// (since we can't assign to a constant, we will initialise
// a constant string with the text)
const std::string john3 = "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
std::string phrase ;
std::cout << "enter a word or phrase: " ;
std::getline( std::cin, phrase ) ;
constauto sz = phrase.size() ; // number of characters in the 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.
std::size_t count = 0 ; // #times the word/phrase was found in the text.
std::size_t find_from = 0 ; // position in john3 to star the search for phrase
std::size_t found_at = 0 ; // position in john3 where phrase was found
while( ( found_at = john3.find( phrase, find_from ) ) != std::string::npos )
{
++count ;
// the next search starts from the position immediately after
// where the phrase that was found this time ended.
// note: this means that if we were searching for "baba" in "abc bababa xyz",
// "baba" would be found just once
find_from = found_at + sz ;
}
// note that this is somewhat naive: for instance
// the phrase "heir dee" would be found twice,
// but the phrase "their deeds" would not be found
// and the phrase "Light" would be found just once.
std::cout << "#times the phrase was found: " << count << '\n' ;
}
Hello @Mwega, @JLBorges is using "std::" as a standard usage. If you want to omit the "std::", you may wanna try what @juliabrushett had done to her code. She incorporated: