strstr() Runtime Error - HELP!

Discussion in 'C++' started by arshad115, Mar 10, 2009.

  1. arshad115

    arshad115 New Member

    Joined:
    Mar 10, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  2. arshad115

    arshad115 New Member

    Joined:
    Mar 10, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.....
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    I have added for you and you can edit after some post counts
     
  4. arshad115

    arshad115 New Member

    Joined:
    Mar 10, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thanks.....
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice