C Program to Check for Uppercase, Lowercase Alphabetic Characters using Switch Statement


Hi this is Anil from Learning Lad and welcome
to another video tutorial. in this video we will see how we can write
a simple program where we will be asking the user to enter an alphabetical character and
when the user is gonna enter that alphabetical character we will say whether he has entered
the uppercase alphabetical character or the lowercase alphabetical character. Here in this video I will be using the C programming
language you can do the same thing in the C++ also. and you guys can write this program in many
ways and in this video we are going to use the switch statement and in the switch statements
case values, instead of specifying a constant value we will be specifying a range of values. now here let’s get started. I have already created the skeleton of this
program. I have included this stdio.h header file which
allows us to use the functions like printf scanf etc and then we have written this main
function here which is the entry point of our program. now here in this program first let us declared
some of the variables that we are going to use. The first variable that we need is for storing
the character entered by the user and since we are working with the character data type
my variables type will be of type char and then I need to give a name. I will say ch and this is the only variable
that we need for this program. now next what we are gonna do is we are gonna
ask the user to enter an alphabetical character and we are going to display a message saying
that and we are going to do that using the printf function. so I will use the printf function and here
I will say please enter an alphabetical character and then i’m gonna insert a new line here
and then after seeing this message user is gonna enter a character. we are going to read that character and we
are going to store that in this ch variable. so I will be used in the scanf function and
here since we are reading a value of type character type the format specifier will be
%c and here I need to specify the address of the Ch variable. For this scanf function to work properly we
need to provide the address of the variable where we want to store the information. Here we want to store the information read
into this variable ch and that’s why we are using this ampersand symbol with this ch variable
which gets the address of this Ch variable. All right. now we have displayed a message asking the
user to enter an alphabetical character and when the user is gonna enter that character,
we are reading that character and we are storing that in this ch variable. now the next thing that we are going to do
is we are going to check whether the user has entered the uppercase character or the
lower case character. now let’s talk about the types of input that
the user can provide here. 1) The user will enter and uppercase alphabetical
character. 2) The user is gonna enter a lowercase alphabetical
character. 3) The user may enter character which may
not be an alphabetical character. so we will check for all these three conditions
and we will display the appropriate message. so as they said before we will be using the
switch statement here. so it’s gonna be switch and in the switch
statements expression part we will have the Ch variable and then i’m gonna use this
curly braces to create the body for this switch statement. Now here the thing is this switch statement
expression value must evaluate to an integer value and here if you using the variables
then you can use the variables of type integer and also you can use the variables of type
characters. Here you can use the variables of type characters
that’s because when we use the characters in the programming the ASCII values of those
characters are used and that’s why we can specify the character variables in the switch
statements expression part. All right. now here inside switch statement body we are
going to write the cases. The first case will be for the uppercase alphabetical
characters. so here instead of specifying a constant value
in the case value part, I will be specifying a range of values. now I have already made a tutorial explaining
how we can use the range of values in the switch statements case values. if you haven’t watched that tutorial then
I highly recommend you guys to watch that and here to specify a range of values in the
switch statements case value part, first we need to write the keyword case and then we
need to leave a space and then we need to specify the lower limit of the range and here
we will be writing this case for the characters from A to Z. So all the values from A to Z will be the
uppercase alphabetical characters. So we can specify that here. like this. A in single quotes that’s because we write
the characters in single quotes and after that I need to leave a space and then I need
to add three dots and then again a space and then I need to specify the upper limit of
this range. So this range is from A to Z. So I will specify that upper limit here and
after that I can add a colon. Now here you as i said before, when we use
the characters in programming, the ASCII values of these characters are used and this range
is like the range of values from 65 to 90. That’s because the ASCII value for the alphabetical
character A is 65 in decimal and the ASCII value for the alphabetical character Z is
90 in decimal. These values are in the ascending order. That is the A’s ASCII value in decimal is
65 and then B’s ASCII value in decimal is 66. C’s ASCII value in decimal is 67 and like
that and similarly the Z’s ASCII value in decimal is actually 90. so these values are in the ascending order. so we can specify the range here. now if the user has entered a character which
falls in this range then that means that the user has entered the uppercase alphabetical
character. so we will display a message saying that. so I will use a printf functions and here
I will say %c is in uppercase and then I need to specify the ch variable here. For example if the user is gonna enter let’s
say a character M then we are displaying like M is in uppercase. so here for this placeholder %c we are specifying
the ch variable here. now after this statement we don’t want execute
any other statements. So we will be adding this break statement,
so that after executing this statement the control can come out of the switch statement
and execute rest of the other statements. So now we have written a case for the uppercase
alphabetical characters. similarly we need to write the case for the
lowercase alphabetical characters and to do that i’m gonna copy these three statements
and i’m gonna past it here and then I’m going to change these values from a to z. Now here for the character a the ASCII value
in decimal is 97 and for the character b the ASCII value in decimal is 98 and and it is
in the ascending order for all that lowercase alphabetical characters. so for the character z the ASCII value is
122. so this range is for the ASCII values in decimal
from 97 to 122 which is nothing but for the characters a to z. so here we will say %c is in lowercase and
then we are going to refer this ch variable here. So if the user is gonna enter the lowercase
alphabetical character then this will display the message. for example if the user has entered let’s
say l then it will say l is in lowercase. so now we have written two cases. one is for the uppercase alphabetical characters
and another one is for the lowercase alphabetical characters. now previously I have also talked about what
if the user is gonna enter a non alphabetical character. for that also we want to display a message
and how we can do that here. now all of you know that in switch statement,
if the switch expressions value doesn’t match with any of the case values then if there
is a default case then that default case statement will be executed. so here in this program if the user has entered
a non alphabetical character then that will not match with this one and it will not match
with the second case also. so here we can have a default case which will
be executed if the user is gonna enter a non alphabetical character. so I’ll have this default case and then for
this default case I will have statement. You know displaying a message so here i’m
gonna use the printf function and i’m gonna say please enter an alphabet. ok So this is the message that we are gonna display
if the user is gonna enter a non alphabetical character. now after this I’m not gonna use the break
statement. that’s because since this is the last part
of the switch statement, after executing the statements there is no other statement to
execute inside is switch. so the program will obviously come out of
this switch statement and execute the rest of the Other statements. so in switch statement for the last case you
don’t need to use the break statement. now what i’m gonna do is i’m gonnar save
this program and i’m gonna build and run this. So let’s do build and run and it says please
enter an alphabetical character. now I’m gonna enter let’s say K and if I
hit the enter button here it says K is in uppercase and i’m gonna close this program. I’m gonna run it again. This time I’m gonna enter a and if I hit the
enter button it says s is in lowercase. of course. I’m gonna close it. I’m gonna run it one more time. Build and run and this time I’m gonna enter
let’s say a number 3 and if i hit the enter button it says please enter an alphabet. so its working properly. I’m gonna run it one more time and this
time i’m gonna enter a symbol like @ and if I hit the enter button it says please enter
an alphabet. so it means that this program is working properly. now here I have used the C programming language
to write this program. you can do the same thing in the c++ you can specify a range of values in the switch
statements case value in C++ also and you know if you are writing the c++ program here
then you just need to change the printf with the cout and then you need to change this
scanf with the cin So you know you will be able to write the
program in the c++ also. now you can write the program for this problem
in many ways and this is one of the way. so this is it guys. this is how you guys can write a simple program
to ask the user for an alphabetical character and then you guys can say whether he has entered
the uppercase character or the lowercase alphabetical character. now if you like this video hit the like button. if you don’t like it then hit the dislike
button. if you want to watch more tutorials like this
then do subscribe and if you want to say something, write that in the comment box and thank you
for watching and I’ll see you later in the next video

Leave a Reply

Your email address will not be published. Required fields are marked *