std::string str1, str2; // 2 strings, input them:
std::cin >> str1 >> str2;
// no quotation marks here, look in both strings
if (str1.find(str2) || str2.find(str1))
std::cout << "Success" << std::endl; // Found a match
else
std::cout << "Failure" << std::endl; // No matches
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
bool one_contains_the_other( const std::string& first, const std::string& second )
{
// check if the shorter string is a substring of the longer one
// find returns std::string::npos on failure
if( first.size() < second.size() ) return second.find(first) != std::string::npos ;
elsereturn first.find(second) != std::string::npos ;
}
bool one_contains_the_other( constchar* first, constchar* second )
{
if( first == nullptr || second == nullptr ) returntrue ;
// forward to the C++ version
elsereturn one_contains_the_other( std::string(first), second ) ;
}
bool one_contains_the_other_2( constchar* first, constchar* second )
{
if( first == nullptr || second == nullptr ) returntrue ;
// check if the shorter string is a substring of the longer one
// strstr returns nullptr on failure
elseif( std::strlen(first) < std::strlen(second) ) return std::strstr(second,first) ;
elsereturn std::strstr(first,second) ;
}
bool one_contains_the_other_3( constchar* first, constchar* second )
{
if( first == nullptr || second == nullptr ) returntrue ;
// make first the longer and second the shorter of the two strings,
if( std::strlen(first) < std::strlen(second) ) std::swap(first,second) ;
for( ; *first ; ++first ) // hancoded strstr
{
if( *first == *second ) // if the first characters match, check for the rest
{
constchar* s = second ;
for( constchar* p = first ; *p && *s && *p == *s ; ++p, ++s ) ;
if( *s == 0 ) returntrue ; // reached the null character, matched
}
}
returnfalse ;
}