0

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;

}
4
Contributors
7
Replies
9
Views
5 Years
Discussion Span
Last Post by Ali_2101
0

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.

0

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

0

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

0

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

0

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);
  
}
0

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

Edited by learner guy: n/a

This article has been dead for over six months. 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.