TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
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:

Go to the source code of this file.

Functions

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

Detailed Description

Search for words in a long textual paragraph.

Definition in file text_search.cpp.

Function Documentation

◆ lower()

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

function to convert a C++ string to lower case

Parameters
wordtakes an std::string as input
Returns
std::string

Definition at line 19 of file text_search.cpp.

19 {
20 int length = word.length();
21 std::string lc = "";
22
23 for (int i = 0; i < length; i++) {
24 lc += tolower(word[i]);
25 }
26
27 return lc;
28}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 44 of file text_search.cpp.

44 {
45 test(); // run self-test implementations
46 std::string paragraph;
47 std::cout << "Please enter your paragraph: \n";
48 std::getline(std::cin, paragraph);
49 std::cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
50 std::cout << "\nThe size of your paragraph = " << paragraph.size()
51 << " characters. \n\n";
52
53 if (paragraph.empty()) {
54 std::cout << "\nThe paragraph is empty" << std::endl;
55 } else {
56 int ch = 0;
57 while (true) {
58 std::string word;
59 std::cout << "Please enter the word you are searching for: ";
60 std::getline(std::cin, word);
61 std::cout << "Ignore case-sensitive? 1 = Yes, 0 = No" << std::endl;
62 std::cin >> ch;
63 if (ch == 1) {
64 std::string lowerCase = lower(
65 paragraph); // convert std::string paragraph to lowercase
66 // and store it in std::string lowerCase
67 std::string lowerCaseWord =
68 lower(word); // convert std::string paragraph to lowercase
69 // and store it in std::string lowerCase
70
71 std::cout << "Hello, your word is " << word << "!\n";
72 if (lowerCase.find(lowerCaseWord) == std::string::npos) {
73 std::cout << word << " does not exist in the sentence"
74 << std::endl;
75 } else {
76 std::cout << "The word " << word
77 << " is now found at location "
78 << lowerCase.find(lowerCaseWord) << std::endl
79 << std::endl;
80 }
81 } else {
82 std::cout << "Hello, your word is " << word << "!\n";
83 if (paragraph.find(word) == std::string::npos) {
84 std::cout << word << " does not exist in the sentence"
85 << std::endl;
86 } else {
87 std::cout << "The word " << word
88 << " is now found at location "
89 << paragraph.find(word) << std::endl
90 << std::endl;
91 }
92 }
93 std::cout << "\nPress Ctrl + C to exit the program.\n\n";
94 std::cin.get();
95 }
96 }
97 return 0;
98}
std::string lower(std::string word)
function to convert a C++ string to lower case
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 34 of file text_search.cpp.

34 {
35 assert(lower("abcd").compare("abcd") == 0);
36 assert(lower("abc").compare("abcd") == -1);
37 assert(lower("abcd").compare("abc") == 1);
38}
int compare(const void *a, const void *b)