1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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:
1 2 3 |
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:
1 2 3 |
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:
1 2 3 |
Enter a character: 5 Using pass-by-value: 5 is not an alphabet. Using pass-by-reference: 5 is not an alphabet. |