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