Я пытаюсь написать функцию strstr с нуля. Я проходил мой код построчно в отладчике, и он отлично работает. Тем не менее, он не сохраняет начало подстроки, которую он ищет должным образом. И, следовательно, он не возвращает его должным образом. У меня нет большого опыта программирования, поэтому мой код немного запутан и запутан. Тем не менее, это работает по большей части. Вот мой код ниже (прокомментировал для моего профессора и для всех вас, чтобы увидеть, что я сделал). (также мой профессор уже выразил свое согласие с функцией goto)
char *strgstr(const char *str1, const char *str2)
{
//I went through this function line by line with the debugger
//The only problem with it is when I go to save the location of the
//substring in str1.
//I posted a question on stackoverflow and I was able to get it to compile
//but it still doesn't save and return properly. The rest of the function works.
int len_str1=strlen(str1);
int len_str2=strlen(str2);
char *save_str=NULL;
int i=0;
for(; i<len_str1; i++)
{
there:
if(str1[i]==str2[0]) //checks if this is the beginning of str2
{
save_str=(char*)str1[i]; //This is where the problem is.
int j=0; //start at beginning of str2
for(;i<len_str1;i++) //index str1
{
if(j<len_str2) //checks if we've finished searching str2
{
if(str1[i]!=str2[j])
{
goto there;
}
j++;
}
else
{
return save_str; //does not return properly. I can't figure out how to save a particular point in the index to a pointer.
}
}
}
}
}