[C言語] 文字種判定関数(isdigit, isnumber, isxdigit, ishexnumber, isblank, isspace, islower, isupper, isalpha, isalnum, isgraph, iscntrl, isascii)

これらの関数はロケール依存なので注意

void printChars(int (*isXXXXX)(int))
{
  int totalChars = 0;
  for (int i = 0; i <= 0xFFFF; i ++) {
    if ((*isXXXXX)(i)) {
      assert(i <= 0xFF);
      if (isgraph(i)) {
        printf("%c", i);
      } else {
        printf("%d,", i);
      }

      totalChars++;
    }
  }

  printf(" Total = %d\n", totalChars);
}

int main()
{
  /*
   [0-9] にマッチ
   */
  // 0123456789 Total = 10
  printChars(isdigit);

  /*
   isdigit に同じ
   */
  // 0123456789 Total = 10
  printChars(isnumber);

  /*
   [0-9a-fA-F] にマッチ
   */
  // 0123456789ABCDEFabcdef Total = 22
  printChars(isxdigit);

  /*
   isxdigit に同じ
   */
  // 0123456789ABCDEFabcdef Total = 22
  printChars(ishexnumber);

  /*
   9 : \t(水平タブ),  32 : 半角スペース
   */
  // 9,32 Total = 2
  printChars(isblank);

  /*
   9:\t, 10:\n(LF), 11:\v(垂直タブ), 12:\f(ページ送り), 13:\r(CR), 32:半角スペース
   */
  // 9,10,11,12,13,32 Total = 6
  printChars(isspace);

  /*
   [a-z] にマッチ
   */
  // abcdefghijklmnopqrstuvwxyz Total = 26
  printChars(islower);

  /*
   [A-Z] にマッチ
   */
  // ABCDEFGHIJKLMNOPQRSTUVWXYZ Total = 26
  printChars(isupper);

  /*
   [a-zA-Z] にマッチ
   */
  // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz Total = 52
  printChars(isalpha);

  /*
   [0-9a-zA-Z]にマッチ
   */
  // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz Total = 62
  printChars(isalnum);

  /*
   印字可能な文字にマッチ
   */
  // !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO
  // PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ Total = 94
  printChars(isgraph);

  /*
   制御文字にマッチ
   */
  // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
  // 18,19,20,21,22,23,24,25,26,27,28,29,30,31,127 Total = 33
  printChars(iscntrl);

  /*
   ASCII文字
   */
  // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27
  // 28,29,30,31,32,!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ
  // [\]^_`abcdefghijklmnopqrstuvwxyz{|}~127," Total = 128
  printChars(isascii);

  return 0;
}
Share
関連記事