#How to make my strchr work with unicode ?

16 messages · Page 1 of 1 (latest)

deep stag
#

I'm trying to reproduce the strchr function, but it doesn't work with unicode.

{
    char    *ptr;

    ptr = (char *) s;
    if (c > 255)
        return (ptr);
    while (*ptr != c)
    {
        if (*ptr == '\0')
            return (NULL);
        ptr++;
    }
    return (ptr);
}


#include <stdio.h>
#include <string.h>

int main()
{
  const char s[] = "īœ˙ˀ˘¯ˇ¸¯.œ«‘––™ª•¡¶¢˜ˀ";
  printf("%s\n", strchr(s, 171));
  printf("%s\n", ft_strchr(s, 171));
  return (0);
}```
sterile frigateBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

quartz ice
#

;compile -Wall -Wextra ```c
#include <stddef.h>
char *ft_strchr(const char *s, int c)
{
char *ptr;

ptr = (char *) s;
if (c > 255)
    return (ptr);
while (*ptr != c)
{
    if (*ptr == '\0')
        return (NULL);
    ptr++;
}
return (ptr);

}

#include <stdio.h>
#include <string.h>

int main()
{
const char s[] = "īœ˙ˀ˘¯ˇ¸¯.œ«‘––™ª•¡¶¢˜ˀ";
printf("%s\n", strchr(s, 171));
printf("%s\n", ft_strchr(s, 171));
return (0);
}

tulip emberBOT
#
Compilation successful

No output.

quartz ice
#

there's no unicode warnings?

#

well anyway, c chars are defined to be 1 byte

#

they can't fit a unicode character

deep stag
#

these are the outputs when i'm running the code

#

first one is the strchr function in the libc, second one is mine

deep stag
stray shuttle
#

first you need convert int c to char

deep stag
#

ok... that was it 💀

#

thank you really much aha

stray shuttle
#

😉

deep stag
#

!solved