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 | #include <iostream> using namespace std; // Function to check if a character is a vowel or consonant using pass-by-value bool isVowelByValue(char ch) { // Convert character to lowercase for uniform comparison ch = tolower(ch); // Check if the character is a vowel return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); } // Function to check if a character is a vowel or consonant using pass-by-reference void isVowelByReference(char ch, bool &isVowel) { // Convert character to lowercase for uniform comparison ch = tolower(ch); // Check if the character is a vowel isVowel = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); } int main() { char ch; bool isVowelByRef; // Input the character cout << "Enter an alphabet: "; cin >> ch; // Check if the character is a vowel or consonant using pass-by-value bool isVowelByVal = isVowelByValue(ch); cout << "Using pass-by-value: "; if (isVowelByVal) { cout << ch << " is a vowel." << endl; } else { cout << ch << " is a consonant." << endl; } // Check if the character is a vowel or consonant using pass-by-reference isVowelByReference(ch, isVowelByRef); cout << "Using pass-by-reference: "; if (isVowelByRef) { cout << ch << " is a vowel." << endl; } else { cout << ch << " is a consonant." << endl; } return 0; } |
Explanation:
- Pass-by-Value (
isVowelByValue
):- Function Definition:
bool isVowelByValue(char ch)
- This function takes the character
ch
as a parameter by value. - It converts the character to lowercase using
tolower
for uniform comparison. - It checks if
ch
is one of the vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’). - It returns
true
if the character is a vowel, otherwisefalse
.
- Function Definition:
- Pass-by-Reference (
isVowelByReference
):- Function Definition:
void isVowelByReference(char ch, bool &isVowel)
- This function takes the character
ch
by value and the resultisVowel
by reference. - It converts the character to lowercase and checks if it is a vowel.
- The result is stored in the
isVowel
reference variable.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input an alphabet character.
- It checks and displays whether the character is a vowel or consonant using both methods.
Example Outputs:
Example 1:
Input:
- Character:
E
Output:
1 2 3 | Enter an alphabet: E Using pass-by-value: E is a vowel. Using pass-by-reference: E is a vowel. |
Example 2:
Input:
- Character:
b
Output:
1 2 3 | Enter an alphabet: b Using pass-by-value: b is a consonant. Using pass-by-reference: b is a consonant. |
Example 3:
Input:
- Character:
G
Output:
1 2 3 | Enter an alphabet: G Using pass-by-value: G is a consonant. Using pass-by-reference: G is a consonant. |