-2

C++ Code. Example: x: "This is #my first program"; y: "#my";

bool function(string x, string y)
{
//Return true if y is contained in x

return ???; 
}

3 Answers3

2

You can use std::string::find()

bool function(string x, string y)
{
    return (x.find(y) != std::string::npos);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

You can do this using string::find

OMGtechy
  • 7,935
  • 8
  • 48
  • 83
0

How the function can be written depends on whether the search for an empty string will be considered as successful or not. If to consider that an empty string is present in any string then the function will look as

bool function( const std::string &x, const std::string string &y )
{
    return ( x.find( y ) != std::string::npos );
}

If to consider that the search of an empty string shall return false then the function will look as

bool function( const std::string &x, const std::string string &y )
{
    return ( !y.empty() && x.find( y ) != std::string::npos );
}

I would prefer that the function would return false for an empty string.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335