0

Lets say I have this string

const char* ch = "ب";
printf("%zu\n", strlen(ch));

result is 2
how I count the unicode "ب" as 1 character ?

Answer

int
name_len(const char *ch)
{
    int i = 0;
    int j = 0;
    while (ch[i] != '\0') {
        if ((ch[i] & 0xC0) != 0x80)
            ++ j;
        ++ i;
    }
    return j;
}
  • You should count by yourself. What character code are you using? (UTF-8, UTF-16, etc.) – MikeCAT Dec 11 '20 at 13:25
  • I'm using UTF-8 –  Dec 11 '20 at 13:26
  • Use a unicode library like ICU, libunistring, etc. – Shawn Dec 11 '20 at 13:30
  • Portably, you can use the `mbrlen()` [function](https://www.cplusplus.com/reference/cwchar/mbrlen/) and count the chars yourself. If on Windows, you can use the `_mbstrlen()` [function](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strlen-wcslen-mbslen-mbslen-l-mbstrlen-mbstrlen-l?view=msvc-160) – Mark Benningfield Dec 11 '20 at 13:33

0 Answers0