Thread: strstr() Runtime Error - HELP!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    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; 03-10-2009 at 08:18 AM. Reason: forgot the code tags!...;p

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You seem to have quite a few loops inside each other, more than what I expected.

    I'm fairly sure you go over the end of the string in this bit:
    Code:
    						for(int l=i;l<x;l++)
    						{
    							if(s1[o+i]==s2[o])
    							{
    								z++;
    							}
    							o++;
    						}
    o will be x-1, and if i = x-1 (which happens at the end of the string), then s[o+i] is way beyond the end of the string.

    I also think you need to break the loop if s1[o+i] != s2[o] and start over at the beginning of s2. Otherwise, strsrt("AaBbCc", "abc") will return &s1[1];

    Some minor code review comments:
    Code:
    						return (char *)(s1+i);
    No need for a cast here.

    Code:
    	char *p=NULL;
    ...
    return p;
    p is never set to anything other than NULL, so why not just return NULL?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  3. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  4. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM