【C言語】ctype.hを使って文字列の判別と操作をする【標準ライブラリ】

【C言語】ctype.hを使って文字列の判別と操作をする【標準ライブラリ】

ctype.hとは?

ctype.h はC言語の標準ライブラリの一つで、主に文字の分類(判定)や変換を行うための関数を提供します。 例えば、ある文字が英字かどうか、数字かどうか、大文字か小文字かなどを判定することができます。 また、文字の大文字・小文字変換も可能です。

文字の分類(判定)

ctype.h には、文字の種類を判定する関数がいくつか用意されています。 これらの関数は、すべて整数(通常はcharまたはint)を引数として受け取り、判定結果を真(非ゼロ)または偽(ゼロ)で返します。

英字かどうかを判定: isalpha()


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

int main() {
    char c = 'A';
    if (isalpha(c)) {
        printf("%c は英字です。\n", c);
    } else {
        printf("%c は英字ではありません。\n", c);
    }
    return 0;
}

数字かどうかを判定: isdigit()


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

int main() {
    char c = '5';
    if (isdigit(c)) {
        printf("%c は数字です。\n", c);
    } else {
        printf("%c は数字ではありません。\n", c);
    }
    return 0;
}

英数字かどうかを判定: isalnum()


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

int main() {
    char c = 'B';
    if (isalnum(c)) {
        printf("%c は英数字です。\n", c);
    } else {
        printf("%c は英数字ではありません。\n", c);
    }
    return 0;
}

空白文字かどうかを判定: isspace()

isspace() は、スペース、タブ、改行などの空白文字を判定します。


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

int main() {
    char c = ' ';
    if (isspace(c)) {
        printf("これは空白文字です。\n");
    } else {
        printf("これは空白文字ではありません。\n");
    }
    return 0;
}

文字の変換

ctype.h には、文字の変換を行う関数も用意されています。

大文字を小文字に変換: tolower()


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

int main() {
    char c = 'A';
    printf("%c の小文字は %c です。\n", c, tolower(c));
    return 0;
}

小文字を大文字に変換: toupper()


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

int main() {
    char c = 'b';
    printf("%c の大文字は %c です。\n", c, toupper(c));
    return 0;
}

実践的な例

英数字のみを抽出するプログラム


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

int main() {
    char str[] = "Hello, World! 123.";
    printf("英数字のみ: ");
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalnum(str[i])) {
            putchar(str[i]);
        }
    }
    printf("\n");
    return 0;
}

文字列をすべて大文字に変換


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

int main() {
    char str[] = "Hello, World!";
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
    printf("大文字変換: %s\n", str);
    return 0;
}

文字列をすべて小文字に変換


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

int main() {
    char str[] = "Hello, World!";
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }
    printf("小文字変換: %s\n", str);
    return 0;
}

パスワードの強度チェック


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

bool is_strong_password(const char *password) {
    bool has_digit = false, has_upper = false, has_lower = false;
    for (int i = 0; password[i] != '\0'; i++) {
        if (isdigit(password[i])) has_digit = true;
        if (isupper(password[i])) has_upper = true;
        if (islower(password[i])) has_lower = true;
    }
    return has_digit && has_upper && has_lower;
}

int main() {
    char password[] = "Hello123";
    if (is_strong_password(password)) {
        printf("強いパスワードです。\n");
    } else {
        printf("弱いパスワードです。\n");
    }
    return 0;
}
コメントは受け付けていません。