I tried to write implementation of the strchr()
.
char *ft_strchr(const char *s, int c)
{
(unsigned char)c;
while (*s)
{
if (*s == c)
return ((char *)s);
s++;
}
if (c == '\0')
return ((char *)s);
return (NULL);
}
It works similar in most of cases except this one:
printf("%s\n", strchr("Whats up with you, men", ' up'));
strchr
returns
p with you, men
while me version ft_strchr
returns NULL
I'm curious why strchr
behaves which way. ' up'
converted to unsigned char
is 'p' (as int
538998128).
Also I found here different implementation and it behave as strchr even with this data.
char *ft_strchr(const char *s, int c) {
while (*s != (unsigned char) c) {
if (!*s)
return NULL;
s++;}
return (char *)s;
}
Also my version is different I guess they should work similar but they don't. I really interested why?
Thanks in advance.