CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Exclamation strstr() Runtime Error - HELP!

    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!

    here is my code:
    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;
    }
    Last edited by arshad115; March 10th, 2009 at 09:18 AM. Reason: forgot the code tags!...;p

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured