Create function strstr / Return pointer in C

4

I'm trying to redo the function strstr (search for a value passed by a parameter in a text, if it finds it shows the rest) per account.

This is my current code, it already came to work but I gave some miss click and stopped working for some reason.

I think you're making some mis-statement on pointers / array

follow the code

/* strstr.c */
#include <stdio.h>
#include <string.h>

char *ft_strstr(char *str, char *to_find)
{   
    int index;
    int aux2;
    char *aux3;

    index = 0;
    aux2 = 0;
    aux3 = 0;



    while (str[index] != '
/* strstr.c */
#include <stdio.h>
#include <string.h>

char *ft_strstr(char *str, char *to_find)
{   
    int index;
    int aux2;
    char *aux3;

    index = 0;
    aux2 = 0;
    aux3 = 0;



    while (str[index] != '%pre%')
    {

           if (to_find[aux2] == str[index]) {
                aux3[aux2] = str[index];
                  aux2++;
                  index++;

                if (to_find[aux2] == '%pre%')
                {
                    while (str[index] != '%pre%')
                    {
                        aux3[aux2] = str[index];
                        aux2++;
                        index++;

                        if (str[index] == '%pre%')
                        {
                         return aux3;
                        }
                    }
                }
            }
        index++;
        }

    return (0);
}

int main(void)
{
    /* char *psResultado;
    char sFrase[] = "isto e um teste";

    printf("\nEndereço Inicial = %s", sFrase );

     A função retornará o endereço correspondente à localização do "to" 
    psResultado = strstr(sFrase, "vasc");

    printf("\nEndereço inicial para a pesquisa = %s\n", psResultado );
    printf("\nEndereço inicial para a pesquisa = %s\n", psResultado );
    */

    printf("%s",ft_strstr("Testando","st"));

   return 0;
}
') { if (to_find[aux2] == str[index]) { aux3[aux2] = str[index]; aux2++; index++; if (to_find[aux2] == '%pre%') { while (str[index] != '%pre%') { aux3[aux2] = str[index]; aux2++; index++; if (str[index] == '%pre%') { return aux3; } } } } index++; } return (0); } int main(void) { /* char *psResultado; char sFrase[] = "isto e um teste"; printf("\nEndereço Inicial = %s", sFrase ); A função retornará o endereço correspondente à localização do "to" psResultado = strstr(sFrase, "vasc"); printf("\nEndereço inicial para a pesquisa = %s\n", psResultado ); printf("\nEndereço inicial para a pesquisa = %s\n", psResultado ); */ printf("%s",ft_strstr("Testando","st")); return 0; }
    
asked by anonymous 31.10.2017 / 09:20

1 answer

1

First we have this:

    aux2 = 0;
    aux3 = 0;

Then we have this:

                aux3[aux2] = str[index];

That is, you are trying to write something on a null pointer.

To properly implement strstr , you should neither create new strings nor write anything in any of the strings. In addition, your program seems to be much more complicated than necessary.

The appropriate solution would be to do this:

  • Go through all positions i of str until it ends (returning NULL ) or until to_find is found.

  • For each position i in str , move through j to_find positions together with i + j str positions until a different character is found or up to to_find terminate (and in this case the i address is returned). One must be careful not to exceed the end of any of the strings.

Here's the code that does this:

#include <stdio.h>

const char *ft_strstr(const char *str, const char *to_find) {
    if (str[0] == 0 && to_find[0] == 0) return str;
    for (int i = 0; str[i]; i++) {
        int j;
        for (j = 0; to_find[j] && str[i + j] && to_find[j] == str[i + j]; j++);
        if (to_find[j] == 0) return &(str[i]);
    }
    return NULL;
}

int main(void) {
    printf("1: %s\n", ft_strstr("Testando", "st"));
    printf("2: %s\n", ft_strstr("O rato roeu a roupa do rei de Roma", "ro"));
    printf("3: %s\n", ft_strstr("Nao vai achar", "vai nada"));
    printf("4: %s\n", ft_strstr("Vai achar no fim", "fim"));
    printf("5: %s\n", ft_strstr("Logo no inicio vai ser encontrado", "Logo no inicio"));
    printf("6: %s\n", ft_strstr("Nao vai procurar nada", ""));
    printf("7: %s\n", ft_strstr("", "Vai procurar em lugar nenhum"));
    printf("8: %s\n", ft_strstr("", ""));
    return 0;
}

All tests of the main function give the expected result:

1: stando
2: roeu a roupa do rei de Roma
3: (null)
4: fim
5: Logo no inicio vai ser encontrado
6: Nao vai procurar nada
7: (null)
8: 

The if of the beginning of the function ft_strstr serves to deal with case 8 above, which is a special case. The rest of the function is the implementation of the algorithm I described above.

See here working on ideone.

    
31.10.2017 / 13:22