mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-24 10:33:10 +08:00
feat: Modify search/text_search.cpp (#1662)
* Modified search/text_search.cpp * Added tests * Added a few test cases * Added a few more test cases and documentation * Minor fix * Minor fixes * Minor fixes * Minor output fixes * Minor output fixes * Minor readability fixes * clang-format and clang-tidy fixes fora01765a6* Restored original settings * clang-format and clang-tidy fixes for6a8f3a4eCo-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
* \file
|
||||
* \brief Search for words in a long textual paragraph.
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#ifdef _MSC_VER
|
||||
@@ -10,9 +11,38 @@
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
/** Main function
|
||||
/**
|
||||
* @brief function to convert a C++ string to lower case
|
||||
* @param word takes an std::string as input
|
||||
* @returns std::string
|
||||
*/
|
||||
std::string lower(std::string word) {
|
||||
int length = word.length();
|
||||
std::string lc = "";
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
lc += tolower(word[i]);
|
||||
}
|
||||
|
||||
return lc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
assert(lower("abcd").compare("abcd") == 0);
|
||||
assert(lower("abc").compare("abcd") == -1);
|
||||
assert(lower("abcd").compare("abc") == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
test(); // run self-test implementations
|
||||
std::string paragraph;
|
||||
std::cout << "Please enter your paragraph: \n";
|
||||
std::getline(std::cin, paragraph);
|
||||
@@ -23,20 +53,46 @@ int main() {
|
||||
if (paragraph.empty()) {
|
||||
std::cout << "\nThe paragraph is empty" << std::endl;
|
||||
} else {
|
||||
int ch = 0;
|
||||
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;
|
||||
std::cout << "Ignore case-sensitive? 1 = Yes, 0 = No" << std::endl;
|
||||
std::cin >> ch;
|
||||
if (ch == 1) {
|
||||
std::string lowerCase = lower(
|
||||
paragraph); // convert std::string paragraph to lowercase
|
||||
// and store it in std::string lowerCase
|
||||
std::string lowerCaseWord =
|
||||
lower(word); // convert std::string paragraph to lowercase
|
||||
// and store it in std::string lowerCase
|
||||
|
||||
std::cout << "Hello, your word is " << word << "!\n";
|
||||
if (lowerCase.find(lowerCaseWord) == 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 "
|
||||
<< lowerCase.find(lowerCaseWord) << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "The word " << word << " is now found at location "
|
||||
<< paragraph.find(word) << std::endl
|
||||
<< std::endl;
|
||||
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::cout << "\nPress Ctrl + C to exit the program.\n\n";
|
||||
std::cin.get();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user