fork download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. uint8_t utf8CharacterLength( char const c )
  5. {
  6. return
  7. (c & 0b10000000) == 0b00000000 ? 1 :
  8. (c & 0b11100000) == 0b11000000 ? 2 :
  9. (c & 0b11110000) == 0b11100000 ? 3 :
  10. (c & 0b11111000) == 0b11110000 ? 4 :
  11. 0;
  12. }
  13.  
  14. int main()
  15. {
  16. char str[] = "ab€cdéefègh";
  17.  
  18. size_t len = strlen( str );
  19.  
  20. for ( size_t i = 0; i < len; i++ )
  21. {
  22. uint8_t len = utf8CharacterLength( str[i] );
  23. printf( "Length of character at pos %2d is %d byte(s)\n", i, len );
  24. i += len-1;
  25. }
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Length of character at pos  0 is 1 byte(s)
Length of character at pos  1 is 1 byte(s)
Length of character at pos  2 is 3 byte(s)
Length of character at pos  5 is 1 byte(s)
Length of character at pos  6 is 1 byte(s)
Length of character at pos  7 is 2 byte(s)
Length of character at pos  9 is 1 byte(s)
Length of character at pos 10 is 1 byte(s)
Length of character at pos 11 is 2 byte(s)
Length of character at pos 13 is 1 byte(s)
Length of character at pos 14 is 1 byte(s)