#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:
- 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 ifch
is an uppercase alphabet, andislower
to check ifch
is a lowercase alphabet. - It prints the result directly, indicating whether
ch
is an uppercase alphabet, a lowercase alphabet, or not an alphabet.
- Function Definition:
- Pass-by-Reference (
checkCaseByReference
):- Function Definition:
void checkCaseByReference(char ch, string &caseType)
- This function takes the character
ch
by value and a reference to astring
variablecaseType
. - It determines if
ch
is an uppercase alphabet, a lowercase alphabet, or not an alphabet, and updates thecaseType
reference variable accordingly. - The result is updated through the reference variable.
- Function Definition:
- 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:
- Character:
G
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:
- Character:
m
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:
- Character:
5
Output:
Enter a character: 5 Using pass-by-value: 5 is not an alphabet. Using pass-by-reference: 5 is not an alphabet.