fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int strlen_u8(const char* str)
  5. {
  6. int I = 0, J = 0;
  7.  
  8. while(str[I])
  9. {
  10. if ((str[I] & 0xC0) != 0x80)
  11. {
  12. ++J;
  13. }
  14. ++I;
  15. }
  16. return J;
  17. }
  18.  
  19. int strlen_s_u8(const char* str, unsigned int size)
  20. {
  21. unsigned int I = 0, J = 0;
  22. while(I < size)
  23. {
  24. if ((str[I] & 0xC0) != 0x80)
  25. {
  26. ++J;
  27. }
  28. ++I;
  29. }
  30. return J;
  31. }
  32.  
  33. int main()
  34. {
  35. const char* str = u8"ゴールデンタイムラバー/スキマスイッチ";
  36.  
  37. std::setlocale(LC_ALL, "ja_JP.UTF-8");
  38. std::cout<<strlen_u8(str)<<"\n";
  39. std::cout<<strlen_s_u8(str, strlen(str))<<"\n";
  40. }
Success #stdin #stdout 0s 5044KB
stdin
Standard input is empty
stdout
19
19