[ create a new paste ] login | about

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

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

using namespace std;

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

int main() {

  string source_str = "ThisWillRockYourWorld";
  string search_str = "AndMaybeMineToo";
  if (containsStr(source_str, search_str))
      cout<< "You need to bite your tongue." <<endl;
   else 
      cout<< "I will bite my tongue." <<endl;

  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

	const auto src_end = sourceStr.end(), dst_end = searchStr.end();

	for (auto src_it = sourceStr.begin(), dst_it = searchStr.begin(); src_it < src_end; ++src_it)
	{
		if (*src_it == *dst_it)
		{
			++src_it;
			for (auto dst_it2 = dst_it + 1; dst_it2 < dst_end; ++dst_it2, ++src_it)
			{
				if (*dst_it2 != *src_it)
					break;
			}
			return true;
		}
	}
	return false;
}


Output:
1
2
3
In function 'bool containsStr(const std::string&, const std::string&)':
Line 26: error: ISO C++ forbids declaration of 'src_end' with no type
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: