Site icon T4Tutorials.com

C++ Program to check whether a character is an uppercase or lowercase alphabet. Using user define functions.

#include <iostream>
using namespace std;

// Function to check if a character is uppercase or lowercase using pass-by-value
void checkCaseByValue(char ch) {
    if (isupper(ch)) {
        cout << ch << " is an uppercase alphabet." << endl;
    } else if (islower(ch)) {
        cout << ch << " is a lowercase alphabet." << endl;
    } else {
        cout << ch << " is not an alphabet." << endl;
    }
}

// Function to check if a character is uppercase or lowercase using pass-by-reference
void checkCaseByReference(char ch, string &caseType) {
    if (isupper(ch)) {
        caseType = "uppercase alphabet";
    } else if (islower(ch)) {
        caseType = "lowercase alphabet";
    } else {
        caseType = "not an alphabet";
    }
}

int main() {
    char ch;
    string caseByRef;

    // Input the character
    cout << "Enter a character: ";
    cin >> ch;

    // Check the case using pass-by-value
    cout << "Using pass-by-value: ";
    checkCaseByValue(ch);

    // Check the case using pass-by-reference
    checkCaseByReference(ch, caseByRef);
    cout << "Using pass-by-reference: " << ch << " is a " << caseByRef << "." << endl;

    return 0;
}

Explanation:

  1. Pass-by-Value (checkCaseByValue):
    • Function Definition: void checkCaseByValue(char ch)
    • This function takes the character ch as a parameter by value.
    • It uses isupper to check if ch is an uppercase alphabet, and islower to check if ch is a lowercase alphabet.
    • It prints the result directly, indicating whether ch is an uppercase alphabet, a lowercase alphabet, or not an alphabet.
  2. Pass-by-Reference (checkCaseByReference):
    • Function Definition: void checkCaseByReference(char ch, string &caseType)
    • This function takes the character ch by value and a reference to a string variable caseType.
    • It determines if ch is an uppercase alphabet, a lowercase alphabet, or not an alphabet, and updates the caseType reference variable accordingly.
    • The result is updated through the reference variable.
  3. Main Function (main):
    • The program prompts the user to input a character.
    • It then checks and displays whether the character is an uppercase alphabet, a lowercase alphabet, or not an alphabet using both methods.

Example Outputs:

Example 1:

Input:

Output:

Enter a character: G
Using pass-by-value: G is an uppercase alphabet.
Using pass-by-reference: G is a uppercase alphabet.

Example 2:

Input:

Output:

Enter a character: m
Using pass-by-value: m is a lowercase alphabet.
Using pass-by-reference: m is a lowercase alphabet.

Example 3:

Input:

Output:

Enter a character: 5
Using pass-by-value: 5 is not an alphabet.
Using pass-by-reference: 5 is not an alphabet.

 

Exit mobile version