私が気にする100の事象

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

isupper関数

概要

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

定義

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

int isupper (int c);
機能 内容
第1引数 int 検査対象の文字
返り値 int 大文字なら真(0以外)、
それ以外なら偽(0)

仕様

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

7.4.1.11 The isupper function
Synopsis
1 #include <ctype.h>
int isupper(int c);
Description
2 The isupper function tests for any character that is an uppercase letter or is one of a locale-specific set of characters for which none of iscntrl, isdigit, ispunct, or isspace is true. In the "C" locale, isupper returns true only for the uppercase letters (as defined in 5.2.1).

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

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

使用例

#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", isupper(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                 
7                

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

コンパイラ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

実装例

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

最終更新日: 2018-05-22