C++ Find Substring from String
Posted by Samath
Last Updated: January 05, 2017

This program compares two strings and returns true if string1 is a substring of string2.

#include <iostream>
#include <string>
using namespace std;

bool IsSubString(string s1, string s2)
{
   int len1 = s1.length();
   int len2 = s2.length();
   if(len1 < len2)
   {
     for(int i =0 ; i < len2-len1+1; i++)
     {
        if(s1.compare(s2.substr(i,len1)) == 0)
          return true;
     }
   }
   return false;
}

int main ()
{
  string s1("Hello World");
  string s2("Wor");
  if(IsSubString(s2,s1))
    cout << "Sub String \n";
  return 0;
}