[ create a new paste ] login | about

Link: http://codepad.org/icUZ8vp1    [ raw code | output | fork ]

C++, pasted on Jan 14:
#include <string>
#include <iostream>

using namespace std;

bool containsStr(const string& sourceStr, const string& searchStr);

int main() {

  string source_str = "finger";
  string search_str = "finger";
  bool found_finger = containsStr(source_str, search_str);
  if (found_finger) 
      cout<< "We found your finger. And now we will eat it..." <<endl;
  else
      cout<< "Your finger is disgusting. I'm not going to eat it." <<endl;
  
  source_str = "Eat your finger";
  found_finger = containsStr(source_str, search_str);
  if (found_finger)
      cout<< "I just ate my finger!" <<endl;
  else 
      cout<< "Ow! I missed my finger, and ate my whole hand instead!";

  return 0;

}

bool containsStr(const string& sourceStr, const string& searchStr) {

    if (searchStr.size() > sourceStr.size()) 
        return false; //since it is not possible for source_str to fully contain search_str since it is shorter

    bool just_broke = false;
    for (unsigned int i = 0; i <= sourceStr.size() - 1; i++) {
        if (sourceStr.at(i) == searchStr.at(0)) {
            //then search for the rest of the characters of search_str in source_str
            for (unsigned int i2 = 1; i2 <= searchStr.size() - 1; i2++) {
                if (i < sourceStr.size() - 1) {
                    if (i2 == 1) { //at first loop
                        //then compare search string's second character with source string's character after the current one
                        if ((searchStr.at(i2)) != (sourceStr.at(i + 1))) {
                            just_broke = true;
                            break; //stop looping through inner loop since we didn't find a match yet
                        }
                    }

                    else { //no longer at second character of search_str
                         //then compare search string's current character with source string's current character
                         if ((searchStr.at(i2)) != (sourceStr.at(i))) {
                             just_broke = true;
                             break; //stop looping through inner loop since we didn't find a match yet
                         }
                    }
                    i++; //increment outer loop counter
                }

                else if (i == sourceStr.size() - 1) {
                    if ((searchStr.at(i2)) != (sourceStr.at(i))) 
                        return false; //since the last character was not the same

                    else
                        return true;

                }
            }
            if (!just_broke)
                return true; //since if it made it to this statement, then there was definitely a full match of search_str in source_str

        }
    }

    return false;

}


Output:
1
2
Your finger is disgusting. I'm not going to eat it.
Ow! I missed my finger, and ate my whole hand instead!


Create a new paste based on this one


Comments: