Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create text_search.cpp #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Searching_Methods/text_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* \file
* \brief Search for words in a long textual paragraph.
*/
#include <cstdlib>
#include <iostream>
#ifdef _MSC_VER
#include <string> // required for MS Visual C++
#else
#include <cstring>
#endif

/** Main function
*/
int main() {
std::string paragraph;
std::cout << "Please enter your paragraph: \n";
std::getline(std::cin, paragraph);
std::cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
std::cout << "\nThe size of your paragraph = " << paragraph.size()
<< " characters. \n\n";

if (paragraph.empty()) {
std::cout << "\nThe paragraph is empty" << std::endl;
} else {
while (true) {
std::string word;
std::cout << "Please enter the word you are searching for: ";
std::getline(std::cin, word);
std::cout << "Hello, your word is " << word << "!\n";
if (paragraph.find(word) == std::string::npos) {
std::cout << word << " does not exist in the sentence"
<< std::endl;
} else {
std::cout << "The word " << word << " is now found at location "
<< paragraph.find(word) << std::endl
<< std::endl;
}
std::cin.get();
}
}
}