isalpha関数を使うことにより、引数で渡した文字が英字であるかどうかをチェックすることができます。
以下にisalpha関数を使用したC言語サンプルコードと実行結果を記します。
isalpha関数の書式は以下の通りです。
#include <ctype.h>
int isalpha(int c);
以下にisalpha関数を使用したC言語サンプルコードを記します。
また実行結果も記します。
isalpha.c (改行コードLF)
#include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { char *ascii = "AB?#12ab"; int i; for (i=0; i<strlen(ascii); i++) { if (isalpha(ascii[i])) { printf("ascii[%d] = %c is alphabet.\n", i, ascii[i]); } else { printf("ascii[%d] = %c is *not* alphabet.\n", i, ascii[i]); } } return 0; }
上記のC言語サンプルコードをコンパイルし、実行した時の結果を以下に記します。
$ gcc isalpha.c -o isalpha $ ./isalpha ascii[0] = A is alphabet. ascii[1] = B is alphabet. ascii[2] = ? is *not* alphabet. ascii[3] = # is *not* alphabet. ascii[4] = 1 is *not* alphabet. ascii[5] = 2 is *not* alphabet. ascii[6] = a is alphabet. ascii[7] = b is alphabet.
以上、isalpha関数を使用したCサンプルコードでした。