In this post, you will learn how to Check if a Character is an Alphabet or not using C++.
This lesson will teach you how to find if a character is an alphabet or not, with decision making statements, logical statements and comparison statements using the C++ Language. Let’s look at the below source code.
How to Check if a Character is an Alphabet or not?
RUN CODE SNIPPETSource Code
#include<iostream> using namespace std; int main() { char ch; cin>>ch; cout<<"Enter a Character: "<<ch<<endl; if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) cout<<"\n"<<ch<<" is an Alphabet"; else cout<<"\n"<<ch<<" isn't an Alphabet"; return 0; }
Input
A
Output
Enter a Character: A A is an Alphabet
#include <iostream>
- This line which is called the header file.Â
#include
statement tells the compiler to use available files and<iostream>
is the name of the specific that we have used in this code. The<iostream>
file stands for Input and Output statement.
using namespace std;
- The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
int main();
- This line usually controls the function of the code, as it calls the functions to perform their tasks.
- The
int main()
shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
{ }
- The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
- Every statement and value between these braces belong to the main function.
The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.
- First we declare the variable ch as a character with the statement char. We then collect the input from the user and store it in the character using function
cin>>
and display the value usingcout<<
 and the Insertion Operators ‘<<‘ , ‘>>’. - Using the comparison operators we declare conditions to be satisfied
(ch>='a' && ch<='z')
,the ‘&&’ is the logical operator AND, which means that both the conditions should be satisfied within the bracket. - To explain the above condition statement; first we have declared the condition that the character should be greater than and equal to ‘a’ and less than and equal to ‘z’, which includes the range of all the alphabets. Similarly we declare a condition for the Capital letters.
- Both of these statements are joined by the logical operator OR ‘||’, this means that any one of the conditions can be satisfied.
- When the condition is satisfied the assigned output statement is displayed, if not the output assigned to the else statement is displayed
- Using the output statement
cout
 display the answer. - End the function with
return 0;
this returns the function to main( ).
Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.