Problem Solving: Detecting Letter Casing

Learn to write a program to check letter casing using functions.

In this lesson, we will learn to check if a letter is small or capital using functions.

Capital or small letter

Take a character input from the user and determine whether the character is a capital letter or a small letter.

Sample input

A

Sample output

Capital letter

Although we discussed this problem in the “Memory, Operations, and Control Structure” chapter. we will solve the same problem here using functions.

So let’s say we have a symbol variable as char input and we need to determine whether it is a capital or small letter. We will create a function charType() that takes symbol as a parameter and returns an integer based on whether the letter is small or capital. If it’s capital, our program will return 1; if it’s small, it’ll return 2; otherwise, it’ll return 3.

Press + to interact
int charType(char symbol)
{
if(symbol >= 'A' && symbol <= 'Z')
return 1;
else if(symbol >= 'a' && symbol <= 'z')
return 2;
else
return 3;
}

To visualize the above discussion, we have an interactive animation for you:

We have determined symbol so far. Next, we need to print the char type on the console.

We will create another printCharType() function that will take the return value from charType() and pass it to the printCharType() as a parameter. This function will use switch-case to check if the value is 1, and if so, case 1 will print Capital letter. If the value is 2, case 2 will print Small letter. Otherwise case 3 will print None.

Press + to interact
void printCharType(int type)
{
switch(type)
{
case 1:
cout << "Capital letter" << endl; break;
case 2:
cout << "Small letter" << endl; break;
case 3:
cout << "None" << endl; break;
}
}

Have you noticed one thing? We are creating and suggesting you create separate functions for computing and displaying output.

Let’s write the complete code below and run it to see the output:

#include <iostream>
using namespace std;
/*This function will get a symbol from user and return that whether the symbol is capital(1)/Small(2) letter or non-alphabet(3). */
int charType(char symbol);
//This function will just print the result depending on int(Type)
void printCharType(int type);
int main()
{
    char symbol;
    cin >> symbol;
    int type = charType(symbol);
    printCharType(type);
    return 0;
}
void printCharType(int type)
{
    switch(type)
    {
        case 1:
            cout << "Capital letter" << endl; break;
        case 2:
            cout << "Small letter" << endl;   break;
        case 3:
            cout << "None" << endl;    break;
    }
}
int charType(char symbol)
{
    if(symbol >= 'A' && symbol <= 'Z')
        return 1;
    else if(symbol >= 'a' && symbol <= 'z')
        return 2;
    else
        return 3;
}





Determining the letter case: Capital or small letter
  • In lines 27–35, we take symbol as a function parameter to determine whether it lies in the range A-Z or a-z. If symbol is a capital letter, we will return 1, and if it is a small letter, we will return 2. Otherwise, we will return 3.

  • In lines 11–12, the value returned from the charType() function is passed as a parameter to the printCharType() function.

  • In lines 15–26, we take type as a function parameter to print whether it is a capital or small letter.

Identify characters from the stream

Write a program that takes as input a stream of characters (ending with the $ character) and tell the counts of the following:

  1. English letters
  2. Small letters
  3. Capital letters
  4. Digits
  5. Special characters
  6. Vowels

First, we will identify the character from the stream of the input. We can simply make a function characterType() in which we will do the following:

Press + to interact
int characterType(char sym)
{
if(sym == 'a' || sym == 'e' || sym == 'i' || sym == 'o' || sym == 'u')//small vowels
return 1;
if(sym == 'A' || sym == 'E' || sym == 'I' || sym == 'O' || sym == 'U')//capital vowels
return 2;
if(sym >= 'a' && sym <= 'z') //small alphabets
return 3;
if(sym >= 'A' && sym <= 'Z')//capital alphabets
return 4;
if(sym >= '0' && sym <= '9')//digits
return 5;
return 6; // any special character return 6
}

Next is the main part in which we will take the input symbol sym and pass it to the characterType() function as an argument. This function will identify the symbol and return the integer. We will take this return value in id and use the switch-case to count the letters, digits, or special characters. Let’s write the main() part:

Press + to interact
cin>>sym; // take an input character
while(sym!='$') // Loop will be stopped when input is $
{
int id = characterType(sym); // pass input to the function
switch(id)
{
case 1: vowelsCount++; // count vowels. NOTE: break was not added deliberately
case 3: lettersCount++; smallCount++; break; //count letters and small letters
case 2: vowelsCount++; // count vowels. NOTE: break was not added deliberately
case 4: lettersCount++; capitalCount++; break;//count letters and capital letters
case 5: digitCount++; break;// count digit
case 6: scCount++; break;// count special character
}
cin>>sym;
}

Multiple lines of code can be written on the same line as long as each line of code ends with a semicolon as shown in lines 8, 11, and 13–14.

We have changed the order of case in the switch statement and removed the break statement in case1 and case3. Because small vowels are also letters, more specifically small letters, if we use the break statement in case 1, it will not count the letters and small letters.

So here is a question for you. Why do you think we checked vowels first and then small and capital letters in the characterType() function? Should we follow the same order in characterType() function? If yes, then why?

Let’s write the complete code below:

#include <iostream>
using namespace std;
int characterType(char sym)
{
    if(sym == 'a' || sym == 'e' || sym == 'i' || sym == 'o' || sym == 'u')
        return 1;
    if(sym == 'A' || sym == 'E' || sym == 'I' || sym == 'O' || sym == 'U')
        return 2;
    if(sym >= 'a' &&  sym <= 'z')
        return 3;
    if(sym >= 'A' &&  sym <= 'Z')
        return 4;
    if(sym >= '0' &&  sym <= '9')
        return 5;
    return 6;
}
int main()
{
    int vowelsCount=0,lettersCount=0, capitalCount=0, smallCount=0, digitCount=0, scCount=0;
    char sym;
    cin>>sym;
    while(sym!='$')
    {
        int id = characterType(sym);
        switch(id)
        {
           case 1: vowelsCount++;
           case 3:  lettersCount++; smallCount++;break;
           
           case 2: vowelsCount++; 
           case 4:  lettersCount++; capitalCount++;break;
           
           case 5: digitCount++;break;
           case 6: scCount++;break;

        }
        cin>>sym;
    }
    cout<<"Vowels Count:"<<vowelsCount<<" Letters Count:"<<lettersCount<<" Small Count:"<<smallCount<<" Capital Count:"<<capitalCount<<" Digit Count:"<<digitCount<<" Special Character Count:"<<scCount<<endl;
}
Identify characters count

We hope you enjoyed this problem-solving walk-through.