TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
text_search.cpp
Go to the documentation of this file.
1
5
#include <cassert>
6
#include <cstdlib>
7
#include <iostream>
8
#ifdef _MSC_VER
9
#include <string>
// required for MS Visual C++
10
#else
11
#include <cstring>
12
#endif
13
19
std::string
lower
(std::string word) {
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
}
29
34
static
void
test
() {
35
assert(
lower
(
"abcd"
).
compare
(
"abcd"
) == 0);
36
assert(
lower
(
"abc"
).
compare
(
"abcd"
) == -1);
37
assert(
lower
(
"abcd"
).
compare
(
"abc"
) == 1);
38
}
39
44
int
main
() {
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
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
compare
Definition
huffman.cpp:36
lower
std::string lower(std::string word)
function to convert a C++ string to lower case
Definition
text_search.cpp:19
search
text_search.cpp
Generated by
1.18.0