Advertisement
Guest User

stackoverflow.com/questions/1452721

a guest
Apr 20th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #define SPACE ' '(char)
  5. using namespace std;
  6.  
  7. // Submit text (maximum 500 characters) and store in variable
  8. string text;
  9. string textQuery(string msgText) {
  10.   do {
  11.     cout << msgText << endl;
  12.     getline(cin, text);
  13.   } while (text.size() > 500);
  14.   return text;
  15. }
  16.  
  17. // Query word to search for and store as variable
  18. string word;
  19. string wordQuery(string msgWord) {
  20.   cout << msgWord << endl;
  21.   cin >> word;
  22.   return word;
  23. }
  24.  
  25. // Using loop, run through the text to identify the word
  26. int counter = 0;
  27. bool debugCheck = false;
  28. int searchWord() {
  29.   for (int i = 0; i + 3 < text.size(); i++) {
  30.     char ch_1 = text.at(i);
  31.     char ch_2 = text.at(i + 1);
  32.     char ch_3 = text.at(i + 2);
  33.     char ch_4 = text.at(i + 3);
  34.     cout << i;
  35.  
  36.     if (ch_1 == word.at(0) && ch_2 == word.at(1) && ch_3 == word.at(2) &&
  37.         ch_4 == word.at(3)) {
  38.       counter++;
  39.       debugCheck = true;
  40.     }
  41.   }
  42.  
  43.   return counter;
  44. }
  45. // cout the result
  46. int main() {
  47.   string textUserSubmit =
  48.       textQuery("Please submit text (max 500 characters): ");
  49.   string wordUserSubmit = wordQuery("Please select a word to search for: ");
  50.   int counterResponse = searchWord();
  51.   cout << debugCheck << endl;
  52.   cout << "The number of times is: " << counterResponse << endl;
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement