C++ provides several character classification functions in the cctype
library to check whether a character is a digit, letter, uppercase, lowercase, etc. These functions are useful for input validation, parsing, and formatting strings.
1️⃣ List of Character Classification Functions
The <cctype>
header contains functions for checking character types.
Function | Description |
---|---|
isalpha(c) |
Returns true if c is a letter (A-Z or a-z). |
isdigit(c) |
Returns true if c is a digit (0-9). |
isalnum(c) |
Returns true if c is a letter or digit (A-Z, a-z, 0-9). |
islower(c) |
Returns true if c is a lowercase letter (a-z). |
isupper(c) |
Returns true if c is an uppercase letter (A-Z). |
tolower(c) |
Converts uppercase letter to lowercase. |
toupper(c) |
Converts lowercase letter to uppercase. |
isspace(c) |
Returns true if c is a whitespace (' ' , '\t' , '\n' , etc.). |
ispunct(c) |
Returns true if c is a punctuation character. |
isprint(c) |
Returns true if c is a printable character (not control character). |
📌 All functions return true (non-zero)
if the condition is met, otherwise false (0)
.
2️⃣ Example: Using Character Classification Functions
#include <iostream>
#include <cctype> // Required for character functions
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (isalpha(ch))
cout << ch << " is a letter.\n";
if (isdigit(ch))
cout << ch << " is a digit.\n";
if (isalnum(ch))
cout << ch << " is alphanumeric.\n";
if (islower(ch))
cout << ch << " is a lowercase letter.\n";
if (isupper(ch))
cout << ch << " is an uppercase letter.\n";
if (isspace(ch))
cout << ch << " is a whitespace character.\n";
if (ispunct(ch))
cout << ch << " is a punctuation character.\n";
return 0;
}
🔹 Example Output
Enter a character: A
A is a letter.
A is alphanumeric.
A is an uppercase letter.
3️⃣ Example: Convert Case Using tolower()
and toupper()
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "Uppercase: " << (char)toupper(ch) << endl;
cout << "Lowercase: " << (char)tolower(ch) << endl;
return 0;
}
🔹 Example Output
Enter a character: b
Uppercase: B
Lowercase: b
4️⃣ Example: Count Letters, Digits, and Spaces in a String
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string str;
int letters = 0, digits = 0, spaces = 0;
cout << "Enter a string: ";
getline(cin, str);
for (char ch : str) {
if (isalpha(ch)) letters++;
if (isdigit(ch)) digits++;
if (isspace(ch)) spaces++;
}
cout << "Letters: " << letters << endl;
cout << "Digits: " << digits << endl;
cout << "Spaces: " << spaces << endl;
return 0;
}
🔹 Example Output
Enter a string: Hello 123!
Letters: 5
Digits: 3
Spaces: 1
5️⃣ Summary
✔ isalpha()
→ Checks if a character is a letter.
✔ isdigit()
→ Checks if a character is a digit.
✔ isalnum()
→ Checks if a character is a letter or digit.
✔ isspace()
→ Checks if a character is a space.
✔ toupper()
, tolower()
→ Convert letter case.
Would you like an example for password validation using these functions? 🚀
0 Comments