Create strstr / Pointer return function in C

Asked

Viewed 1,667 times

4

I am trying to redo the strstr function (look for a value passed by parameter in a text, find shows the rest) by account.

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

I believe you are making some wrong statement on the pointers/array

follows 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] != '\0')
    {

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

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

                        if (str[index] == '\0')
                        {
                         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;
}

1 answer

1

First we have this:

    aux2 = 0;
    aux3 = 0;

Then we have this:

                aux3[aux2] = str[index];

I mean, you’re trying to write something on a null pointer.

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

The proper solution would be to do this:

  • Scroll through all positions i of str until it is over (turning back NULL) or until the to_find be found.

  • For each position i in str, scroll through positions j of to_find along with the positions i + j of str until a different character is found or until the to_find end (and in this case return the address of i). Care should be taken not to overshoot the end of any of the strings.

Here’s the code that does it:

#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 the function tests main 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 start of the function ft_strstr serves to handle 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.