[Solved] In plain C, without using strlen or any library function that uses strlen, how do you find whether one string is contained in another?


Removing the variable flag is deceptively easy by the way, as the only cases (using your algorithm as shown) where your function should return true is if either s1 is empty or the condition !*c is true. In all other cases it should return false.

So your function, without any other modifications, could easily be changed to this:

bool contains ( char * s1, char * s2 ) 
{
   // returns true or false depending on whether s1 is contained in s2

   // define that every string contains the empty string
   if ( !*s1 ) return true;

   // search for substrings of s2 that equal s1
   while ( *s2 ) 
   {
       char * c = s1; 
       while ( *c++ == *s2++ ); 
       if ( !*c ) 
            return true;
   }

   return false;
}

2

solved In plain C, without using strlen or any library function that uses strlen, how do you find whether one string is contained in another?