[ create a new paste ] login | about

Link: http://codepad.org/BArNGSZM    [ 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 sourceStr to fully contain searchStr 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)) {
            cout<< "sourceStr.at(" << i << ") == searchStr.at(0).\n"
                   "sourceStr.at(" << i << ") is: " << sourceStr.at(i) << '\n' <<
                   "searchStr.at(" << 0 << ") is: " << searchStr.at(0) <<endl;
            //then search for the rest of the characters of searchStr in sourceStr
            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
                            cout<< "sourceStr.at(" << i + 1 << ") == searchStr.at(" << i2 << ").\n"
                                   "sourceStr.at(" << i + 1 << ") is: " << sourceStr.at(i+1) << '\n' <<
                                   "searchStr.at(" << i2 << ") is: " << searchStr.at(i2) <<endl;
                    }

                    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
                         }
                         else
                            cout<< "sourceStr.at(" << i << ") == searchStr.at(" << i2 << ").\n"
                                   "sourceStr.at(" << i << ") is: " << sourceStr.at(i) << '\n' <<
                                   "searchStr.at(" << i2 << ") is: " << searchStr.at(i2) <<endl;
                    }
                    ++i; //increment outer loop counter
                }

                else if (i == sourceStr.size() - 1) {
                    cout<< "i is sourceStr.size() - 1 (meaning we're at the last character of sourceStr).\n"
                           "i is: " << i << '\n' << "sourceStr.size() - 1 is: " << sourceStr.size() - 1 <<endl;
                    if ((searchStr.at(i2)) != (sourceStr.at(i))) 
                        return false; //since the last character was not the same

                    else {
                        cout<< "sourceStr.at(" << i << ") == searchStr.at(" << i2 << ").\n"
                               "sourceStr.at(" << i<< ") is: " << sourceStr.at(i) << '\n' <<
                               "searchStr.at(" << i2 << ") is: " << searchStr.at(i2) <<endl;
                        return true;
                    }

                }
            }

            if (searchStr.size() == 1)
                return true; //since searchStr was only one character and it had a match - the last character of sourceStr

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

        }
    }

    return false;

}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sourceStr.at(0) == searchStr.at(0).
sourceStr.at(0) is: f
searchStr.at(0) is: f
sourceStr.at(1) == searchStr.at(1).
sourceStr.at(1) is: i
searchStr.at(1) is: i
Your finger is disgusting. I'm not going to eat it.
sourceStr.at(9) == searchStr.at(0).
sourceStr.at(9) is: f
searchStr.at(0) is: f
sourceStr.at(10) == searchStr.at(1).
sourceStr.at(10) is: i
searchStr.at(1) is: i
Ow! I missed my finger, and ate my whole hand instead!


Create a new paste based on this one


Comments: