Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <math.h>
- #define SPACE ' '(char)
- using namespace std;
- // Submit text (maximum 500 characters) and store in variable
- string text;
- string textQuery(string msgText) {
- do {
- cout << msgText << endl;
- getline(cin, text);
- } while (text.size() > 500);
- return text;
- }
- // Query word to search for and store as variable
- string word;
- string wordQuery(string msgWord) {
- cout << msgWord << endl;
- cin >> word;
- return word;
- }
- // Using loop, run through the text to identify the word
- int counter = 0;
- bool debugCheck = false;
- int searchWord() {
- for (int i = 0; i + 3 < text.size(); i++) {
- char ch_1 = text.at(i);
- char ch_2 = text.at(i + 1);
- char ch_3 = text.at(i + 2);
- char ch_4 = text.at(i + 3);
- cout << i;
- if (ch_1 == word.at(0) && ch_2 == word.at(1) && ch_3 == word.at(2) &&
- ch_4 == word.at(3)) {
- counter++;
- debugCheck = true;
- }
- }
- return counter;
- }
- // cout the result
- int main() {
- string textUserSubmit =
- textQuery("Please submit text (max 500 characters): ");
- string wordUserSubmit = wordQuery("Please select a word to search for: ");
- int counterResponse = searchWord();
- cout << debugCheck << endl;
- cout << "The number of times is: " << counterResponse << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement