# Algorithms_in_C++: search/text_search.cpp File Reference

Loading...
Searching...
No Matches
text_search.cpp File Reference

Search for words in a long textual paragraph. More...

`#include <cassert>`
`#include <cstdlib>`
`#include <iostream>`
`#include <cstring>`
Include dependency graph for text_search.cpp:

## Functions

std::string | lower (std::string word)
| function to convert a C++ string to lower case More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

Search for words in a long textual paragraph.

## Function Documentation

## lower()

std::string lower | ( | std::string | word | ) |

function to convert a C++ string to lower case

Parameters

  word | takes an std::string as input

Returns
  std::string

{
int length = word.length();
std::string lc = "";
for (int i = 0; i < length; i++) {
lc += tolower(word[i]);
}
return lc;
}
std::string
std::string::length
T length(T... args)
std::tolower
T tolower(T... args)
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
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 {
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 << "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 << "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;
}
std::cin
std::cout
std::string::empty
T empty(T... args)
std::endl
T endl(T... args)
std::string::find
T find(T... args)
std::getline
T getline(T... args)
lower
std::string lower(std::string word)
function to convert a C++ string to lower case
Definition: text_search.cpp:19
test
static void test()
Self-test implementations.
Definition: text_search.cpp:34
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
assert(lower("abcd").compare("abcd") == 0);
assert(lower("abc").compare("abcd") == -1);
assert(lower("abcd").compare("abc") == 1);
}
compare
Definition: huffman.cpp:28
Here is the call graph for this function: