[C言語 標準ライブラリ] ctype.hの使い方

※本ブログでは、商品・サービスのリンク先にプロモーションを含みます。

スポンサーリンク

ctype.hをインクルードすることで、文字を識別する関数群と大文字⇔小文字に変換する関数が使用可能になります。

スポンサーリンク

関数

int isalnum(int c)

引数が英数字(’A’~’Z’、’a’~’z’、’0’~’9’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int c = 0;

void Func(int c)
{
	if(isalnum(c)){
		printf("英数字です。\n");
	}else{
		printf("英数字ではありません。\n");
	}
}

int main(void)
{
	Func('A');
	Func('z');
	Func('0');
	Func('+');

	return 0;
}

実行結果:

英数字です。
英数字です。
英数字です。
英数字ではありません。

int isalpha(int c)

引数がアルファベット(’A’~’Z’、’a’~’z’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int c = 0;

void Func(int c)
{
	if(isalpha(c)){
		printf("アルファベットです。\n");
	}else{
		printf("アルファベットではありません。\n");
	}
}

int main(void)
{
	Func('A');
	Func('z');
	Func('0');
	Func('+');

	return 0;
}

実行結果:

アルファベットです。
アルファベットです。
アルファベットではありません。
アルファベットではありません。

int isblank(int c)

引数が標準ブランク文字(半角スペース(’ ‘)または水平タブ(’\t’))の場合に真、それ以外の場合に偽を返します。

使用例:

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

int c = 0;

void Func(int c)
{
	if (isblank(c)) {
		printf("標準ブランク文字です。\n");
	}else{
		printf("標準ブランク文字ではありません。\n");
	}
}

int main(void)
{
	Func('A');
	Func('0');
	Func('+');
	Func('\n');
	Func(' ');
	Func('\t');

	return 0;
}

実行結果:

標準ブランク文字ではありません。
標準ブランク文字ではありません。
標準ブランク文字ではありません。
標準ブランク文字ではありません。
標準ブランク文字です。
標準ブランク文字です。

int iscntrl(int c)

引数が制御文字(0x00~0x1F、0x7F)の場合に真、それ以外の場合に偽を返します。制御文字はタブや改行などが該当します。

使用例:

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

int c = 0;

void Func(int c)
{
	if (iscntrl(c)) {
		printf("制御文字です。\n");
	}else{
		printf("制御文字ではありません。\n");
	}
}

int main(void)
{
	Func('A');
	Func('0');
	Func('+');
	Func('\n');
	Func('\t');

	return 0;
}

実行結果:

制御文字ではありません。
制御文字ではありません。
制御文字ではありません。
制御文字です。
制御文字です。

int isdigit(int c)

引数が数字(’0’~’9’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int c = 0;

void Func(int c)
{
	if (isdigit(c)) {
		printf("数字です。\n");
	}else{
		printf("数字ではありません。\n");
	}
}

int main(void)
{
	Func('A');
	Func('0');
	Func('9');
	Func('+');
	Func('\n');

	return 0;
}

実行結果:

数字ではありません。
数字です。
数字です。
数字ではありません。
数字ではありません。

int isgraph(int c)

引数が半角スペース(’ ‘)を除く表示可能な文字の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', ' ', '1', '#', '\t', '\n', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'はスペースを除いたprint可能な文字ですか? %s\n",
               chars[i], isgraph(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’はスペースを除いたprint可能な文字ですか? Yes
‘ ‘はスペースを除いたprint可能な文字ですか? No
‘1’はスペースを除いたprint可能な文字ですか? Yes
‘#’はスペースを除いたprint可能な文字ですか? Yes
‘ ‘はスペースを除いたprint可能な文字ですか? No

‘はスペースを除いたprint可能な文字ですか? No
”はスペースを除いたprint可能な文字ですか? No
スポンサーリンク

int islower(int c)

引数が小文字のアルファベット(’a’~’z’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', '#', ' ', 'z', 'Z', '\n', '\t', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'は小文字か? %s\n",
               chars[i], islower(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’は小文字か? No
‘a’は小文字か? Yes
‘1’は小文字か? No
‘#’は小文字か? No
‘ ‘は小文字か? No
‘z’は小文字か? Yes
‘Z’は小文字か? No

‘は小文字か? No
‘ ‘は小文字か? No
”は小文字か? No

int isprint(int c)

引数が表示可能な文字の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', '#', ' ', 'z', 'Z', '\n', '\t', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'はスペースを含むprint可能な文字ですか? %s\n",
               chars[i], isprint(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’はスペースを含むprint可能な文字ですか? Yes
‘a’はスペースを含むprint可能な文字ですか? Yes
‘1’はスペースを含むprint可能な文字ですか? Yes
‘#’はスペースを含むprint可能な文字ですか? Yes
‘ ‘はスペースを含むprint可能な文字ですか? Yes
‘z’はスペースを含むprint可能な文字ですか? Yes
‘Z’はスペースを含むprint可能な文字ですか? Yes

‘はスペースを含むprint可能な文字ですか? No
‘ ‘はスペースを含むprint可能な文字ですか? No
”はスペースを含むprint可能な文字ですか? No

int ispunct(int c)

引数が区切り文字の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', '#', ' ', 'z', 'Z', '.', '\n', '\t', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'は、句読点文字ですか? %s\n",
               chars[i], ispunct(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’は、句読点文字ですか? No
‘a’は、句読点文字ですか? No
‘1’は、句読点文字ですか? No
‘#’は、句読点文字ですか? Yes
‘ ‘は、句読点文字ですか? No
‘z’は、句読点文字ですか? No
‘Z’は、句読点文字ですか? No
‘.’は、句読点文字ですか? Yes

‘は、句読点文字ですか? No
‘ ‘は、句読点文字ですか? No
”は、句読点文字ですか? No

int isspace(int c)

引数が空白類文字の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', '#', ' ', '\n', '\t', '\v', '\f', '\r', 'z', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'は空白文字ですか? %s\n",
               chars[i], isspace(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’は空白文字ですか? No
‘a’は空白文字ですか? No
‘1’は空白文字ですか? No
‘#’は空白文字ですか? No
‘ ‘は空白文字ですか? Yes

‘は空白文字ですか? Yes
‘ ‘は空白文字ですか? Yes
‘ ’は空白文字ですか? Yes
‘ ’は空白文字ですか? Yes
‘は空白文字ですか? Yes
‘z’は空白文字ですか? No
”は空白文字ですか? No

int isupper(int c)

引数が大文字のアルファベット(’A’~’Z’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', '#', ' ', 'z', 'Z', '.', '\n', '\t', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'は大文字ですか? %s\n",
               chars[i], isupper(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’は大文字ですか? Yes
‘a’は大文字ですか? No
‘1’は大文字ですか? No
‘#’は大文字ですか? No
‘ ‘は大文字ですか? No
‘z’は大文字ですか? No
‘Z’は大文字ですか? Yes
‘.’は大文字ですか? No

‘は大文字ですか? No
‘ ‘は大文字ですか? No
”は大文字ですか? No

int isxdigit(int c)

引数が16進数文字(’0’~’9’、’a’~’f’、’A’~’F’)の場合に真、それ以外の場合に偽を返します。

使用例:

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

int main(void)
{
    char chars[] = {'A', 'a', '1', 'F', 'f', 'G', 'g', '#', ' ', 'z', 'Z', '.', '\n', '\t', '\0'};

    for(int i=0; i<sizeof(chars)/sizeof(chars[0]); i++){
        printf("'%c'は16進数の数字ですか? %s\n",
               chars[i], isxdigit(chars[i]) ? "Yes" : "No");
    }

    return 0;
}

実行結果:

‘A’は16進数の数字ですか? Yes
‘a’は16進数の数字ですか? Yes
‘1’は16進数の数字ですか? Yes
‘F’は16進数の数字ですか? Yes
‘f’は16進数の数字ですか? Yes
‘G’は16進数の数字ですか? No
‘g’は16進数の数字ですか? No
‘#’は16進数の数字ですか? No
‘ ‘は16進数の数字ですか? No
‘z’は16進数の数字ですか? No
‘Z’は16進数の数字ですか? No
‘.’は16進数の数字ですか? No

‘は16進数の数字ですか? No
‘ ‘は16進数の数字ですか? No
”は16進数の数字ですか? No

int tolower(int c)

引数が大文字アルファベットの場合、小文字アルファベットに変換し、それを返します。
引数が大文字アルファベット以外の場合、引数をそのまま返します。

使用例:

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

int main(void)
{
    char str[] = "Hello, WoRLd!";
    int len = strlen(str);

    for(int i=0; i<len; i++){
        str[i] = tolower(str[i]);
    }

    printf("%s\n", str);

    return 0;
}

実行結果:

hello, world!

int toupper(int c)

引数が小文字アルファベットの場合、大文字アルファベットに変換し、それを返します。
引数が小文字アルファベット以外の場合、引数をそのまま返します。

使用例:

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

int main(void)
{
    char str[] = "Hello, WoRLd!";
    int len = strlen(str);

    for(int i=0; i<len; i++){
        str[i] = toupper(str[i]);
    }

    printf("%s\n", str);

    return 0;
}

実行結果:

HELLO, WORLD!
スポンサーリンク
C言語

コメント