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:

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

Recommended Answers

All 16 Replies

What's the error you are getting.

Error(msg BOX):
Assignment 3_Question 2.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

When you doesn't find the string function astrstr returns a null pointer which is not a bad thing;
So, you need to this -

char *ptr = astrstr;
if(ptr)
cout<<*ptr;

When you doesn't find the string function astrstr returns a null pointer which is not a bad thing;
So, you need to this -

char *ptr = astrstr;
if(ptr)
cout<<*ptr;

your code is totally wrong,it will never work!

Yes it seems I was in a hurry but I hope you understand the point.

Instead of simply
cout<<astrstr((s,t);

collect the pointer and then check whether it is null or not.

char *ptr=astrstr(s,t);
if(ptr)
cout<<ptr;

Because your function astrstr is returning null pointer i.e. for string
"this is a simple string\0"
pointer which you are returning in case of failure is pointing to last charachter which is '\0' .

Hmm, this makes me thinking about an override somewhere in your program ...

What happens if you change:

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

To:

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

????

it gives the same error!...
btw i checked the string.h function and it also gave a runtime error for the same values......thats weird!

With me your program just compiles and works perfectly, you're maybe using another compiler ...
You've luck you aren't running Win9x, otherwise you risk a system crash instead of a program crash ;) ...

lol...i m using Visual Studio express edition 2008

Maybe you should try compiling with MinGW instead, your code is 100% working !!!

Maybe you should try compiling with MinGW instead, your code is 100% working !!!

i havent used it,but i will give it a try!

BUG REPORT!
Seems like your program doesn't want the following string: char str1[] = "test hello aatttttttttttttttttttttttttttttttttttttttttttttttt"; Here's the code:

#include <iostream>

using namespace std;

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 main(void)
{
    char str1[] = "test hello aatttttttttttttttttttttttttttttttttttttttttttttttt";
    char str2[] = "hello";

    cout << "C++ function: " << strstr(str1, str2) << endl;
    cout << "My function: " << astrstr(str1, str2) << endl;

    return 0;
}

The standard C++-function gives the right output, however your function's output is nothing ...

The error is probably located where the function returns its value
(line 35 in the code I posted in the previous message) ...

BUG REPORT!
Seems like your program doesn't want the following string:
char str1[] = "test hello aatttttttttttttttttttttttttttttttttttttttttttttttt";
The standard C++-function gives the right output, however your function's output is nothing ...

my poor function couldn't handle an array of 61 characters!...

i checked it,it does check conditions but somewhere it gives the array out of bound error and so gives a runtime error!

The error is probably located where the function returns its value
(line 35 in the code I posted in the previous message) ...

no line 35 aint a problem,its just type casting the return type to be a pointer!
i have used it in my other functions,works well!

It's a wrong algorithm (bad code started from the second for loop).
Look at working (but not optimized) code:

const char* bstrstr(const char* s, const char* p)
{
    if (s == 0 || p == 0)   // guard
        return 0;
    int slen = strlen(s);   // (astrlen;)
    int plen = strlen(p);   //   -*-*-

    if (plen == 0)  // booster
        return s + slen;
    int k, n = slen - plen + 1;
    for (int i = 0; i < n; ++i, ++s) {
        if (*s == p[0]) {
            for (k = 1; k < plen && s[k] == p[k]; ++k)
                ;
            if (k == plen)
                return s; 
        }
    }
    return 0;
}
inline
char* bstrstr(char* s, const char* p) {
    return const_cast<char*>(bstrstr(
        const_cast<const char*>(s),p));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.