TheAlgorithms-C-Plus-Plus/search/text_search.cpp

43 lines
1.3 KiB
C++
Raw Normal View History

2020-05-29 09:36:11 +08:00
/**
* \file
* \brief Search for words in a long textual paragraph.
*/
2020-05-27 04:40:09 +08:00
#include <cstdlib>
2017-12-24 01:30:49 +08:00
#include <iostream>
2020-05-29 09:36:11 +08:00
#ifdef _MSC_VER
#include <string> // required for MS Visual C++
#else
#include <cstring>
#endif
2017-12-24 01:30:49 +08:00
2020-05-29 09:36:11 +08:00
/** Main function
*/
2020-05-27 04:40:09 +08:00
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";
2017-12-24 01:30:49 +08:00
2020-05-27 04:40:09 +08:00
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;
2017-12-24 01:30:49 +08:00
}
std::cin.get();
2017-12-24 01:30:49 +08:00
}
2019-08-21 10:10:08 +08:00
}
2017-12-24 01:30:49 +08:00
}