fork download
  1. #include <stdio.h>
  2.  
  3. int readUTF8char(char *out) {
  4. int n;
  5. if(scanf("%c",out)!=1)return 0;
  6. if(((*out)&0x80)==0x00)n=0;
  7. else if(((*out)&0xe0)==0xc0)n=1;
  8. else if(((*out)&0xf0)==0xe0)n=2;
  9. else if(((*out)&0xf8)==0xf0)n=3;
  10. else if(((*out)&0xfc)==0xf8)n=4;
  11. else if(((*out)&0xfe)==0xfc)n=5;
  12. else return 0; /* 不正な開始バイト */
  13. while(n--) {
  14. if(scanf("%c",++out)!=1)return 0;
  15. if(((*out)&0xc0)!=0x80)return 0; /* 不正なデータ */
  16. }
  17. *(++out)='\0';
  18. return 1;
  19. }
  20.  
  21. int main(void) {
  22. char buffer[1024];
  23. char buffer2[1024];
  24.  
  25. /* 順番を入れ替えると改行文字を読み込んでしまってうまくいかない */
  26.  
  27. /* UTF-8の1文字を読み込む */
  28. if(!readUTF8char(buffer2))return 1;
  29.  
  30. /* 文字列を読み込む */
  31. if(scanf("%s",buffer)!=1)return 1;
  32.  
  33. printf("%s\n%s\n",buffer,buffer2);
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 2012KB
stdin
猫
漢字
stdout
漢字
猫