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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> using namespace std; // Function to check the type of character using pass-by-value char checkCharByValue(char ch) { if (ch >= 'a' && ch <= 'z') { return 's'; // 's' for small letter } else if (ch >= 'A' && ch <= 'Z') { return 'c'; // 'c' for capital letter } else { return 'x'; // 'x' for special character } } // Function to check the type of character using pass-by-reference void checkCharByReference(char ch, char &type) { if (ch >= 'a' && ch <= 'z') { type = 's'; // 's' for small letter } else if (ch >= 'A' && ch <= 'Z') { type = 'c'; // 'c' for capital letter } else { type = 'x'; // 'x' for special character } } int main() { char ch; char resultByValue, resultByReference; // Input the character cout << "Enter a character: "; cin >> ch; // Check character type using pass-by-value resultByValue = checkCharByValue(ch); cout << "Result using pass-by-value: "; if (resultByValue == 's') { cout << "Small letter" << endl; } else if (resultByValue == 'c') { cout << "Capital letter" << endl; } else { cout << "Special character" << endl; } // Check character type using pass-by-reference checkCharByReference(ch, resultByReference); cout << "Result using pass-by-reference: "; if (resultByReference == 's') { cout << "Small letter" << endl; } else if (resultByReference == 'c') { cout << "Capital letter" << endl; } else { cout << "Special character" << endl; } return 0; } |
Explanation:
- Pass-by-Value (
checkCharByValue
):- Function Definition:
char checkCharByValue(char ch)
- This function receives a copy of the character
ch
and determines its type. - It returns a character indicating the type: ‘s’ for small letter, ‘c’ for capital letter, and ‘x’ for special character.
- The original variable in
main
is not affected by this function.
- Function Definition:
- Pass-by-Reference (
checkCharByReference
):- Function Definition:
void checkCharByReference(char ch, char &type)
- This function receives the character
ch
and a reference to the variabletype
. - It directly modifies the
type
variable based on the character’s type: ‘s’, ‘c’, or ‘x’. - The
type
variable inmain
is updated by this function.
- Function Definition:
- Main Function (
main
):- The program prompts the user to enter a single character.
- It then uses both the pass-by-value and pass-by-reference methods to determine the type of the character.
- Finally, it prints the results for both methods.
Example Outputs:
Example 1: Small Letter
Input:
- Character:
b
Output:
1 2 3 |
Enter a character: b Result using pass-by-value: Small letter Result using pass-by-reference: Small letter |
Example 2: Capital Letter
Input:
- Character:
M
Output:
1 2 3 |
Enter a character: M Result using pass-by-value: Capital letter Result using pass-by-reference: Capital letter |
Example 3: Special Character
Input:
- Character:
@
Output:
1 2 3 |
Enter a character: @ Result using pass-by-value: Special character Result using pass-by-reference: Special character |