Check Whether a Character is an Alphabet: C, C++, Python, Java
Programs in C, C++, Python, and Java to check if a character is an alphabet, covering ASCII range logic, isalpha(), Unicode edge cases, and placement test traps.
A character is an alphabet if it is one of the 52 English letters: uppercase A to Z or lowercase a to z. Checking this is a foundational placement-round problem in C, C++, Python, and Java.
What “Is an Alphabet” Means in Code
English has 26 uppercase letters and 26 lowercase letters. In ASCII encoding, each character maps to a fixed integer:
| Character set | Characters | ASCII range |
|---|---|---|
| Uppercase letters | A to Z | 65 to 90 |
| Lowercase letters | a to z | 97 to 122 |
| Digits | 0 to 9 | 48 to 57 |
| Space | (space) | 32 |
| Common punctuation | !, @, #, [, { | 33 to 47, 58 to 96 |
A character is an alphabet if its ASCII value falls in 65 to 90 or 97 to 122. Everything else (digits, punctuation, whitespace, and control characters) is not an alphabet.
A common source of confusion: several older textbooks and online references list the ranges backwards, saying lowercase is 65 to 90 and uppercase is 97 to 122. That is wrong. Uppercase A is decimal 65. Lowercase a is decimal 97. The gap between Z at 90 and a at 97 is occupied by six punctuation characters: [, \, ], ^, _, and ` (ASCII 91 to 96). A range check written as ch >= 'A' && ch <= 'z' (using uppercase A as the lower bound and lowercase z as the upper bound) would incorrectly pass all six of those punctuation characters as alphabets. Knowing the gap exists is what separates correct code from code that fails boundary MCQs.
Placement coding rounds at service-tier companies (AMCAT Automata, TCS CodeVita filter, eLitmus PE test) test this problem in two forms: as a standalone task, and embedded inside a string where you count alphabets, digits, and special characters separately. The algorithm for both is identical at the character level.
ASCII Range Check in C and C++
The algorithm is a two-condition check: first ask whether the character’s value is between the uppercase bounds, then ask whether it falls between the lowercase bounds. If either condition is true, the character is an alphabet. If neither is true, it is not. The conditions use character literals rather than raw ASCII integers so the code reads as a statement of intent:
/* C: alphabet check using ASCII range */
#include <stdio.h>
int main(void) {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("'%c' is an alphabet.\n", ch);
} else {
printf("'%c' is not an alphabet.\n", ch);
}
return 0;
}
Using character literals ('A', 'Z', 'a', 'z') is equivalent to writing the raw integers 65, 90, 97, and 122. Both compile to the same comparison instructions. The character-literal form survives a code review; the raw-integer form forces the reader to decode the ASCII table in their head.
The same logic in C++ with cin and cout:
// C++: alphabet check using ASCII range
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
cout << "'" << ch << "' is an alphabet." << endl;
} else {
cout << "'" << ch << "' is not an alphabet." << endl;
}
return 0;
}
Sample output for three test cases:
Enter a character: G
'G' is an alphabet.
Enter a character: 7
'7' is not an alphabet.
Enter a character: @
'@' is not an alphabet.
The @ character is a useful boundary test: its ASCII value is 64, one below uppercase A at 65. Any range check that fails on @ has an off-by-one error in the lower bound.
Using isalpha() from ctype.h
C’s standard library provides isalpha() in <ctype.h>, which condenses the two-range check into a single call:
/* C: alphabet check using isalpha() */
#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isalpha((unsigned char)ch)) {
printf("'%c' is an alphabet.\n", ch);
} else {
printf("'%c' is not an alphabet.\n", ch);
}
return 0;
}
Three things every student should know about isalpha() before using it in a placement submission:
- Cast to
unsigned charfirst. The C standard specifies that arguments to functions in<ctype.h>must be representable as anunsigned charor equal toEOF. Passing a plainsigned charthat holds a negative byte value (which happens on platforms wherecharis signed and the input character has a code point above 127) triggers undefined behavior. The cast(unsigned char)chis the standard fix and adds zero runtime cost. - The function is locale-dependent. In the default
"C"locale,isalpha()accepts exactly A to Z and a to z. In a locale likede_DE.UTF-8, it may also return non-zero forü,ö, andä. If the placement problem guarantees ASCII-only input, the locale difference does not matter. If input could come from a locale-aware environment, the explicit range check is safer. - The full
<ctype.h>family.isalpha()belongs to a set of character-classification functions that together cover every type a placement problem might ask about:isdigit()for digits 0 to 9,ispunct()for punctuation,isspace()for whitespace, andisalnum()for letters or digits combined. For a four-way classification problem (uppercase, lowercase, digit, special character) combineisupper(),islower(),isdigit(), and a fallback for everything else. FACE Prep’s article on classifying a character as uppercase, lowercase, number, or special character covers the four-branch version in detail.
The C++ version of isalpha() uses the same header and the same cast; the call syntax is identical to the C version above.
Python and Java Approaches
Python
Python’s str.isalpha() works on a single character or a full string. For a single-character alphabet check:
# Python: alphabet check using str.isalpha()
ch = input("Enter a character: ")
if len(ch) == 1 and ch.isalpha():
print(f"'{ch}' is an alphabet.")
else:
print(f"'{ch}' is not an alphabet.")
The len(ch) == 1 guard prevents "abc".isalpha() from passing as a single-character result when the user types multiple characters. Most placement test environments supply a single character, but the length check costs nothing and prevents wrong output on multi-character input.
The Unicode caveat matters for Python more than for C: 'é'.isalpha() returns True because Python’s string methods follow the Unicode standard, not the ASCII subset. For placement problems where the expected output for é is “not an alphabet” (a common assumption in ASCII-based problem sets), add an ASCII guard:
# Python: strict ASCII-only alphabet check
ch = input("Enter a character: ")
if len(ch) == 1 and ch.isascii() and ch.isalpha():
print(f"'{ch}' is an ASCII alphabet.")
else:
print(f"'{ch}' is not an ASCII alphabet.")
str.isascii() returns True for characters with code point 127 or below. The combined isascii() and isalpha() check restricts acceptance to exactly the 52 English letters.
Java
// Java: alphabet check using Character.isLetter()
import java.util.Scanner;
public class AlphabetCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
if (Character.isLetter(ch)) {
System.out.println("'" + ch + "' is an alphabet.");
} else {
System.out.println("'" + ch + "' is not an alphabet.");
}
}
}
Character.isLetter(ch) returns true for any Unicode letter. Character.isAlphabetic(ch), available since Java 7, is similar but excludes letter-number characters from certain numeral systems. For placement tests that use only standard ASCII input, both methods produce identical results.
For strict ASCII range checking in Java (useful when the problem states “English alphabet only”):
// Java: strict ASCII range check
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
System.out.println("'" + ch + "' is an ASCII alphabet.");
} else {
System.out.println("'" + ch + "' is not an ASCII alphabet.");
}
The condition mirrors the C version exactly. Character literals compile to their integer ASCII values, so this is semantically identical to comparing against 65, 90, 97, and 122.
Edge Cases That Catch Students Off Guard
Placement auto-graders test boundary inputs that look similar to alphabets in casual reading. The six characters between uppercase Z at ASCII 90 and lowercase a at ASCII 97 are the most common traps because a range check written as ch >= 'A' && ch <= 'z' (note the mixed bounds) passes all six as valid alphabets. Test every program against this list before submitting:
| Input | ASCII value | Expected output | Why it is a trap |
|---|---|---|---|
@ | 64 | not alphabet | One below A=65; fails loose lower-bound checks |
A | 65 | alphabet | Lower boundary: first uppercase letter |
Z | 90 | alphabet | Upper boundary: last uppercase letter |
[ | 91 | not alphabet | One above Z=90; passes the mixed-bound range |
\ | 92 | not alphabet | Punctuation gap between Z and a |
] | 93 | not alphabet | Punctuation gap between Z and a |
^ | 94 | not alphabet | Punctuation gap between Z and a |
_ | 95 | not alphabet | Punctuation gap between Z and a |
` | 96 | not alphabet | One below a=97; passes the mixed-bound range |
a | 97 | alphabet | Lower boundary: first lowercase letter |
z | 122 | alphabet | Upper boundary: last lowercase letter |
{ | 123 | not alphabet | One above z=122; fails loose upper-bound checks |
7 | 55 | not alphabet | Digit in the range below A=65 |
é | 233 | method-dependent | isalpha() locale-dependent; Python str.isalpha() returns True |
The most frequently missed case in MCQ options is the mixed-bound range 'A' to 'z'. Choosing the correct split (two separate conditions joined by ||) is what every correctly written solution has in common.
The vowel-or-consonant problem is the natural extension of this check: first confirm the character passes the alphabet test, then classify it as a vowel or a consonant. FACE Prep’s guide on checking whether a character is a vowel or consonant in C, C++, Python, and Java covers that next step including switch-case and set-membership approaches.
Where This Problem Shows Up in Placement Tests
Two variants appear in actual placement coding rounds.
Standalone form. The problem statement reads: “Write a program to determine whether the entered character is an alphabet or not.” Auto-graders test at least three inputs: a letter, a digit, and a special character. Every output branch must produce the correct label and punctuation.
Embedded in a string. The problem statement reads: “Count the number of alphabets, digits, and special characters in a given string.” The single-character check becomes the inner condition of a loop, with three counters that increment based on which branch each character falls into. The classification logic does not change; only the loop and counter variables are added around it.
Getting the boundary conditions right (not just testing a letter and a digit but also the @, [, and ` boundary inputs) is what determines whether all test cases pass or just the sample ones visible in the problem statement. AMCAT Automata and TCS CodeVita filter-round problems at the foundational level grade on the invisible boundary cases.
After these character-classification problems, a solid next step is the C coding questions set 1 article, which covers pointer-level and structure-level MCQs that appear in the same screening rounds. If the coding section includes coded-input problems (where a character is shifted or XOR-masked before the alphabet check runs) the coding and decoding questions in aptitude tests article has worked examples of that pattern.
Character classification is where every text-processing pipeline starts. Before a tokenizer decides where to split a sentence or which tokens to merge, it runs exactly this kind of loop (isalpha(), isdigit(), isspace()) at scale across millions of characters. If you want to see what sits one layer above that isalpha() call, TinkerLLM shows how modern language models tokenize text in a live playground, available from ₹499.
Primary sources
Frequently asked questions
What are the ASCII values of uppercase and lowercase alphabet letters?
Uppercase letters A through Z have ASCII values 65 to 90. Lowercase letters a through z have ASCII values 97 to 122. Any character whose value falls outside both ranges is not a standard English alphabet letter.
What is the difference between isalpha() and checking ASCII ranges manually?
The ASCII range check is explicit and locale-independent: it accepts exactly A-Z (65 to 90) and a-z (97 to 122). isalpha() from ctype.h is shorter, but its result depends on the current locale and may return true for accented characters outside the ASCII alphabet range.
Does Python's isalpha() return True for accented characters like e-acute or u-umlaut?
Yes. Python's str.isalpha() follows Unicode and returns True for any character classified as a letter, including accented Latin letters and script letters from other writing systems. Use ch.isascii() and ch.isalpha() together to restrict the check to standard English A-Z and a-z only.
How is alphabet checking different from isalnum() in C?
isalpha() returns true only for letters (A-Z, a-z). isalnum() returns true for both letters and digits (A-Z, a-z, 0-9). Use isalpha() for a pure alphabet check; use isalnum() when you want to accept digits as well as letters.
Is it safe to pass a signed char or EOF to isalpha() in C?
No. The C standard requires that the argument be representable as an unsigned char or be the value EOF. Passing a plain signed char holding a negative value to isalpha() causes undefined behavior. Always cast: isalpha((unsigned char)ch).
What Java method checks if a character is an alphabet letter?
Character.isLetter(ch) returns true for any Unicode letter. Character.isAlphabetic(ch), available since Java 7, is slightly stricter. For strict ASCII-only placement checks, test the explicit range: ch >= 'A' and ch <= 'Z', or ch >= 'a' and ch <= 'z'.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹499)