strstr() Runtime Error - HELP!

arshad115's Avatar, Join Date: Mar 2009
Newbie Member
Code:
char *astrstr(const char *s1,const  char *s2)
{
	char *p=NULL;

	int x=astrlen(s1);
	int y=astrlen(s2);

	int o=0,z=0;
	for(int i=0;i<x; i++)
	{
		for(int k=0;k<y;k++)
		{
			if(s1[i]==s2[k])
				{	o=0;z=0;	
						for(int l=i;l<x;l++)
						{
							if(s1[o+i]==s2[o])
							{
								z++;
							}
							o++;
						}
			
			
				}
				
				if(z==y)
					{
						return (char *)(s1+i);
				
					}
		}
	}
return p;
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	char s[]={"this is a simple string"};
	char t[]={"is"};
	cout << " S: "<< s << endl << " T: "<< t << endl << endl;
	
	cout  << "My Function: " << endl <<  astrstr(s,t) << endl << " String Function : " << endl << strstr(s,t) << endl  << "S: "<< s << endl << "T: "<< t << endl;
	
	
	
	return 0;
}
arshad115's Avatar, Join Date: Mar 2009
Newbie Member
i have made this strstr() function,it gives the correct answer if it finds the correct string inside the another but the program crashes if it does not find the string,gives a runtime error,compliler does not give any error!

sorry i forgot the code tags,i cant find the edit button.....
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I have added for you and you can edit after some post counts
arshad115's Avatar, Join Date: Mar 2009
Newbie Member
thanks.....
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Is it the function itself that core dumps or the "cout << ... astrstr()" part?
Possible cout << NULL won't go down very well; you can solve this by checking the return value before using it.