
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Whether a Character is Alphabet or Not in C++
Using strings or characters is sometimes very useful while solving some logical programming problems. Strings are a collection of characters and characters are 1-byte datatype to hold symbols from ASCII values. The symbols can be letters from English alphabets, numeric digits, or special characters. In this article, we shall learn how to check whether a character is a letter in English an alphabet or not using C++.
Checking isalpha() function
To check whether a number is an alphabet or not, we can use the isalpha() function from the ctype.h header file. This takes a character as input and returns true when it is the alphabet, otherwise returns false. Let us see the following C++ implementation to understand the usage of this function.
Example
#include <iostream> #include <ctype.h> using namespace std; string solve( char c ) { if( isalpha( c ) ) { return "True"; } else { return "False"; } } int main() { cout << "Is 'K' an alphabet? : " << solve( 'K' ) << endl; cout << "Is 'a' an alphabet? : " << solve( 'a' ) << endl; cout << "Is '!' an alphabet? : " << solve( '!' ) << endl; cout << "Is '5' an alphabet? : " << solve( '5' ) << endl; cout << "Is 'f' an alphabet? : " << solve( 'f' ) << endl; }
Output
Is 'K' an alphabet? : True Is 'a' an alphabet? : True Is '!' an alphabet? : False Is '5' an alphabet? : False Is 'f' an alphabet? : True
By creating our function for checking
The above-mentioned method is using a predefined function that checks whether the given character is an alphabet or not. But the same thing we can implement by defining a function with ranging conditions. The algorithm is given below ?
Algorithm
- read a character c as input
- if ASCII of c is in range lowercase 'a' and 'z' or in range uppercase 'A' and 'Z', then
- otherwise
- return false
- end if
Example
#include <iostream> #include <ctype.h> using namespace std; string solve( char c ) { if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ) { return "True"; } else { return "False"; } } int main() { cout << "Is 'T' an alphabet? : " << solve( 'T' ) << endl; cout << "Is 'g' an alphabet? : " << solve( 'g' ) << endl; cout << "Is '?' an alphabet? : " <<solve( '?' ) << endl; cout << "Is '8' an alphabet? : " << solve( '8' ) << endl; cout << "Is 'p' an alphabet? : " << solve( 'p' ) << endl; }
Output
Is 'T' an alphabet? : True Is 'g' an alphabet? : True Is '?' an alphabet? : False Is '8' an alphabet? : False Is 'p' an alphabet? : True
Conclusion
Checking whether a given character is an alphabet or not, there are a few different methods. The very first method we have discussed is using the isalpha function from ctype.h header file. This will take a character, when it is an alphabet, returns true, otherwise false. In the second method that we have covered, we are writing our function for this checking. This is being checked by checking the ASCII is within the given range of lowercase ?a' to ?z' or uppercase ?A' to ?Z'. If yes, then return true, otherwise false.