A C++ Program to Check Whether an Entered Character is lowercase or Uppercase
Hello Learners,
In this article, we will learn some easy concepts about characters and their ASCII Codes. English Alphabets are either Uppercase or Lowercase. We can easily tell which alphabet character is lowercase and which is uppercase. An interesting fact is that implementing them in the programming field is also not a very tough job to do. We can easily do it by just associating it with some special codes defined in Computer Languages. Those special codes are known as ASCII Codes. So, let’s start.
C++ Program to Check Whether an Entered Character is lowercase or Uppercase
Lowercase & Uppercase Characters :
- a-z are lowercase characters.
- A-Z are Uppercase Characters.
For a-z characters, ASCII Codes are defined in a range of 97-122. That means, a will be having ASCII Code 97, b will have 98, c will have 99………………………………………………., z will have 122.
Similarly, For A-Z characters, ASCII Codes are defined in a range of 65-90. That means, A will be having ASCII Code 65, B will have 66, C will have 67………………………………………………., Z will have 90.
So, if we want to implement this in the program, we will use a “for loop” for doing this job. Loop will check for each and every character if it’s a lowercase character or an uppercase one. We will initialize the loop from 97 to 122 which is a condition for lowercase characters. if the character entered by the user matches with any of them then flag becomes 1 else it will remain 0. A flag is a variable that is used as a boolean. If it’s true then it’ll become 1 else 0 for the false case.
Lastly, if the flag is 1 then we will print Lowercase Character else Uppercase Character.
Let’s implement this in the program:
#include<iostream> using namespace std; int main() { char ch,i,flag=0; cout<<"Enter any character: "; cin>>ch; for(i=97;i<=122;i++) { if(ch==i) { flag=1; break; } } if(flag==1) cout<<"It is a Lowercase Character."; else cout<<"It is an Uppercase Character."; return 0; }
Now, let’s see the Output screen:
Enter any character: a It is a lowercase Character.
Enter any character: Z It is an Uppercase Character.
Enter any character: q It is a lowercase Character.
I hope it was easy enough to understand. Please see my other blog posts as well by just clicking on my name mentioned below the main topic. Feel free to ask your queries in the comment section.
Thank You..!!
Regards,
Codespeedy Tech Pvt Ltd.
Leave a Reply