I want to find if Array B is present in array A(B is a substring of A)..
i have written this small function but its not working as desired ..can anybody plz help and point out where i m wrong

bool substring(char A[],char B[])
{
int aSize=strlen(A);
int bSize=strlen(B);
bool matched =true; 
int count =0;

for (int i=0;i<aSize;)
   {
	   if(A[i]==B[0])
	   {
	     for(int compare= 0;compare< bSize;)
		 {
		   if(A[i+compare]==B[compare])
		   {
		    matched =true;
			compare++;
			
			cout << "eeeee\n";
		   }else
		   {
			  
			matched = false;
			
		        break;
		   }
		 }

		 if(matched == true)
		 {
			 
		  return true;
		 }

       }else
	   {
		   
	    i++;
	   }
   }
return matched;

}

Recommended Answers

All 7 Replies

Initialize matched to false rather than true. Suppose A is "abcde" and B is "xyz" , always first if will be false and ultimately you return matched which is true in your case which is wrong.

Initialize matched to false rather than true. Suppose A is "abcde" and B is "xyz" , always first if will be false and ultimately you return matched which is true in your case which is wrong.

u r right but that doesn't affect the program ..problem is somewhere else

Okay then maybe you would like to post output that you are getting wrong and the expected output as well

regardless of what the contents of arrays are (matching or not) ,its returning false every time ,thats the real trouble bcz apparently code looks fine

its returning false every time , thats the real trouble bcz apparently code looks fine

This returns true. Perhaps you never gave it any decent test data.

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



    bool substring(char A[],char B[])
    {
    int aSize=strlen(A);
    int bSize=strlen(B);
    bool matched =true;
    int count =0;
     
    for (int i=0;i<aSize;)
    {

      if (A[i] == 0  || B[i] == 0)
      {
        // reached end of char string. Stop now.
        matched = false;
        break;
      }
      
        
    if(A[i]==B[0])
    {
    for(int compare= 0;compare< bSize;)
    {
    if(A[i+compare]==B[compare])
    {
    matched =true;
    compare++;
     
    cout << "eeeee\n";
    }else
    {
     
    matched = false;
     
    break;
    }
    }
     
    if(matched == true)
    {
     
    return true;
    }
     
    }else
    {
     
    i++;
    }
    }
    return matched;
     
    }


int main()
{
  char* a = "abcdefghij";
  char* b = "cde";
  
  cout << "\n" <<  substring(a,b);
  
}

its returning false every time , thats the real trouble bcz apparently code looks fine

This returns true. Perhaps you never gave it any decent test data.

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



    bool substring(char A[],char B[])
    {
    int aSize=strlen(A);
    int bSize=strlen(B);
    bool matched =true;
    int count =0;
     
    for (int i=0;i<aSize;)
    {

      if (A[i] == 0  || B[i] == 0)
      {
        // reached end of char string. Stop now.
        matched = false;
        break;
      }
      
        
    if(A[i]==B[0])
    {
    for(int compare= 0;compare< bSize;)
    {
    if(A[i+compare]==B[compare])
    {
    matched =true;
    compare++;
     
    cout << "eeeee\n";
    }else
    {
     
    matched = false;
     
    break;
    }
    }
     
    if(matched == true)
    {
     
    return true;
    }
     
    }else
    {
     
    i++;
    }
    }
    return matched;
     
    }


int main()
{
  char* a = "abcdefghij";
  char* b = "cde";
  
  cout << "\n" <<  substring(a,b);
  
}

thnks for ur reply ,its working fine with initial characters of string but not with later ones e.g.
i tried it with this

char* a = "abcdefghij";
	char* b = "efgh";

and it didn't work,whats the reason

This is wrong

if (A[i] == 0  || B[i] == 0)

Should be replaced by

if (A[i] == 0 )
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.