Problem Solving: Capital Letter and Small Letter
Learn to write a program to check if a letter is capital or small with ASCII values.
In this lesson, we will write a program to see if a letter is capital or small.
So let’s get to it!
Capital or small letter
We will use the ASCII table to solve these problems.
Problem statement
Write and run a program that takes a character input from the user and tells whether the character is a capital letter or a small letter.
Sample input
A
Sample output
Capital Letter
To write a program for the above problem, we first focus on simple logic. The logic is that we need one character
as input and compare that input with ASCII numbers.
Let’s break it down.
Let’s say we have a character
variable to store letters of the alphabet and a symbol
variable to store ASCII values.
To check if character
is a capital letter or a small letter, we need to typecast this character
into a symbol
.
This is what we have so far:
char character;int symbol;cout << "The character: ";cin >> character;symbol = int(character);
Now the central part: how do we perform different operations in the program? How can we compare each symbol
in the if-else
or switch
statement?
Comparing symbol
with its ASCII value
We can write one condition to check capital letters and the second condition to check small letters. We will execute the else
part if both conditions are false.
if(symbol >= 65 && symbol <= 90) // compare symbol with capital letter range 65-90{cout << "Capital letter" << endl;}else if(symbol >= 97 && symbol <= 122) // compare symbol with small letter range 97-122{cout << "Small letter" << endl;}else{cout << "Wrong input" << endl;}
Here are the ASCII ranges of capital and small letters:
Capital and Small Letters ASCII Ranges
Capital letters | ASCII values range |
A–Z | 65–90 |
Small letters | ASCII values range |
a–z | 97–122 |
Now let us write the complete code below and run it to see the output.
#include <iostream> using namespace std; int main() { char character; int symbol; cout << "The character: "; cin >> character; symbol = int(character); // Typecasting character to integer to get ASCII value if(symbol >= 65 && symbol <= 90) // compare symbol with capital letter range 65-90 { cout << "Capital letter" << endl; } else if(symbol >= 97 && symbol <= 122) // compare symbol with capital letter range 65-90 { cout << "Small letter" << endl; } else { cout << "None" << endl; } return 0; }
Helping program to find the ASCII value of a character
You may use the following program if you don’t remember the above ASCII table for characters.
#include <iostream>using namespace std;int main() {// your code goes herechar sym = 'A';cout << "ASCII value of '"<<sym<< "' is : " << int(sym)<<endl;return 0;}
You may change the character symbol of the above program by changing 'A'
to 'Z'
, 'a'
to 'z'
or any character you want to check the ASCII value for.
We will make some changes to the above program and solve this problem using the character
comparison instead of the symbol
.
Comparing character
using if-else
We will make some changes in the if-else
condition. We will change one condition to compare the character
variable with 'A'
and 'Z'
and the second condition to compare the character
variable with 'a'
and 'z'
.
if(character >= 'A' && character <= 'Z') //compare character with capital Letters{cout << "Capital letter" << endl;}else if(character >= 'a' && character <= 'z')//compare character with small Letters{cout << "Small letter" << endl;}
The compiler automatically converts character >= 'a'
to an ASCII comparison of the two characters.
Hence our simplified and readable code will be the following:
#include <iostream> using namespace std; int main() { char character; cout << "enter character: "; cin >> character; if(character >= 'A' && character <= 'Z') //compare character with capital Letters { cout << "Capital letter" << endl; } else if(character >= 'a' && character <= 'z') //compare character with small Letters { cout << "Small letter" << endl; } else { cout << "Non" << endl; } return 0; }