私が気にする100の事象

気にしなければ始まらない。

isalpha関数

概要

C言語で文字がアルファベットか判定する関数です。 アルファベットなら真(0以外)、それ以外なら偽(0)を返します。 <ctype.h>#includeすることによって使うことができます。

定義

定義は次の通りになります。

int isalpha (int c);
機能 内容
第1引数 int 検査対象の文字
返り値 int アルファベットなら真(0以外)、
それ以外なら偽(0)

仕様

ISO/IEC 9899:1999 における仕様を示します。

7.4.1.2 The isalpha function
Synopsis
1 #include <ctype.h>
int islower(int c);
Description
2 The isalpha function tests for any character for which isupper or islower is true, or any character that is one of a locale-specific set of alphabetic characters for which none of iscntrl, isdigit, ispunct, or isspace is true.168) In the "C" locale, isalpha returns true only for the characters for which isupper or islower is true.

また、JIS X 3010:2003 における仕様を示します。

7.4.1.2 isalpha 関数
形式 #include <ctype.h>
int isalpha(int c);
機能 isalpha 関数は,isupper 若しくは islower が真となる文字,又は iscntrl,isdigit,ispunct若しくは isspace のいずれも真とならない文化圏固有のアルファベット文字集合の中の文字かどうかを判定する(168)。"C"ロケールでは,isalpha は,isupper 又は islower が真となる文字に対してだけ真を返す。

使用例

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
    char c;
    system("cls");
    printf("  0123456789ABCDEF");
    for(c = 0x00; c < 0x7F; c++) {
        if (c % 16 == 0) printf("\n%x ", c / 16);
        printf("%s", isalpha(c) ? "\033[41m" : "\033[0m");
        printf("%c", isprint(c) ? c :'.');
        printf("\033[0m");
    }
    printf("\n");
    return 0;
}

結果は以下のようになります。

./islower.exe
  0123456789ABCDEF
0                 
1                 
2                 
3                 
4  ABCDEFGHIJKLMNO
5 PQRSTUVWXYZ     
6  abcdefghijklmno
7 pqrstuvwxyz    

真のときに文字が表示されているように見えますが、 実際には赤い背景色が文字を囲んでいます。

コンパイラEmbarcadero C++ 7.20です。

bcc32c --version
Embarcadero C++ 7.20 for Win32 Copyright (c) 2012-2016 Embarcadero Technologies, Inc.
Embarcadero Technologies Inc. bcc32c version 3.3.1 (35832.6139226.5cda94d) (based on LLVM 3.3.1)
Target: i686-pc-win32-omf
Thread model: posix

実装例

islower関数isupper関数を組み合わせて実装すると簡単に作れます。

int islower (int c) {return 'a' <= c && c <= 'z';}
int isupper (int c) {return 'A' <= c && c <= 'Z';}
int isalpha (int c) {return islower(c) || isupper(c);}
最悪計算時間 最良計算時間 平均計算時間 最悪空間計算量
 O(1)
 O(1)
 O(1)
 O(1)

最終更新日: 2018-05-25