first my task was to write this program that checks that string t contained in string s or not and i successfully made it and now i have to change this program a little bit and now i have to use a bool function in this program any one can tell me that how i will do that. thank you in advance. please help me

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

void find(char str1[], char str2[])
{
     /* Perform the search. */
     char *ptr;
     ptr = strstr(str1, str2);
     
     if ( ptr ==  NULL )
     {
          cout << str1 << " not contained in " << str2 << endl;
          cout << "Again compairing the after removing the "
               << "first character of string 2:" << endl;
          find(str1, str2+1);
             }
     else
     cout << str2 << " contained in " << str1;
     
     }

int main()
{
    char *ptr, str1[80], str2[80];
    
    cout << "Enter the string to be searched: ";
    cin >> str1;
    
    cout << "Enter the target string: ";
    cin >> str2;
    
    find(str1, str2);
    
    return 0;
    }

Recommended Answers

All 6 Replies

Start by determining what your base case is. You have two of them:

  1. str2 is matched in str1
  2. str2 is empty

Once you have the base cases you can return the appropriate result, and then for any non-base case simply return the result of your recursive call: return find(str1, str2+1) .

can you just tell me the match statement how i can write the match statement because i am confused about it please tell me statements

You already have the match statement written. It works. All you need to do is cover both base cases and return the correct boolean value for each base.

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

void find(char str1[], char str2[])
{
     /* Perform the search. */
     char *ptr;
     bool more = true;
     while (more)
     {
     ptr = strstr(str1, str2);
     if ( ptr ==  NULL )
     {
          cout << str1 << " not contained in " << str2 << endl;
          cout << "Again compairing the after removing the "
               << "first character of string 2:" << endl;
             }
     else
     cout << str2 << " contained in " << str1;
     more = false;
     }
     return find(str1, str2+1);
     }

int main()
{
    char *ptr, str1[80], str2[80];
    
    cout << "Enter the string to be searched: ";
    cin >> str1;
    
    cout << "Enter the target string: ";
    cin >> str2;
    
    find(str1, str2);
    
    return 0;
    }

i tried this but it didn't work please guide me

You are missing return for base case.

thanks tonyjv and Narue now i am ready with my code once again thanks to all of guys for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.