Recode strstr en C
evolution
-
KX Mensajes publicados 19031 Estado Moderador -
KX Mensajes publicados 19031 Estado Moderador -
```c
char *my_strstr (char *str, char *to_find)
{
char *pointeur;
int i;
int j;
int counter;
pointeur = NULL;
i = 0;
j = 0;
counter = 0;
while (pointeur == NULL && str[i] != '\0')
{
if (str[i] == to_find[j])
{
j = 0; // Reset j when we start matching
while(str[i + j] == to_find[j])
{
if (to_find[j] == '\0')
return &str[i]; // Found
j++;
}
}
i++;
}
return NULL;
}
```
char *my_strstr (char *str, char *to_find)
{
char *pointeur;
int i;
int j;
int counter;
pointeur = NULL;
i = 0;
j = 0;
counter = 0;
while (pointeur == NULL && str[i] != '\0')
{
if (str[i] == to_find[j])
{
j = 0; // Reset j when we start matching
while(str[i + j] == to_find[j])
{
if (to_find[j] == '\0')
return &str[i]; // Found
j++;
}
}
i++;
}
return NULL;
}
```
2 respuestas
Es tu línea *pointeur = to_find[i]; la que falla.
pointeur es NULL, por lo que *pointeur no es accesible.
Además, to_find[i] es un carácter, no un puntero.
Si quieres desplazar el puntero to_find 'i' posiciones, puedes hacer to_find+i
Al cambiar tu línea por pointeur = to_find+i; parece funcionar en mi ejemplo my_strstr("bonjour","bon"), pero puede que haya otros errores...
--
La confianza no excluye el control.
pointeur es NULL, por lo que *pointeur no es accesible.
Además, to_find[i] es un carácter, no un puntero.
Si quieres desplazar el puntero to_find 'i' posiciones, puedes hacer to_find+i
Al cambiar tu línea por pointeur = to_find+i; parece funcionar en mi ejemplo my_strstr("bonjour","bon"), pero puede que haya otros errores...
--
La confianza no excluye el control.