0

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;
    }
3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by AliRaiz
0

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) .

0

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

0

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.

0
#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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.