C++ Program to find if a character is a Vowel or ConsonantIn this article, we will discuss how to find a character is a vowel or constant in C++. If we want to check whether a letter is either a vowel or a consonant, we can use the program written below:
In C++, there are several methods to determine if a given letter is a vowel or a consonant. The three common methods are switch-case statements, if-else statements, and a simpler method that makes use of arrays. Let us discuss each with an example: Method 1: If-else statementsOutput: Enter a character: a a is a vowel. Enter a character: B b is a consonant. Explanation: In this code, we first convert the Character to lowercase to handle both uppercase & lowercase input. After that, using if-else expressions, we determine whether the Character is one of the vowels ('a', 'e', 'i', 'o', 'u'). Method 2: Using switch-case statementsOutput: Enter a character: e e is a vowel. Enter a character: X x is a consonant. Explanation: In this code, we also convert the Character to lowercase and then use a switch-case statement to check if it matches any of the vowels. Method 3: Using an arrayOutput: Enter a character: i i is a vowel. Enter a character: Y y is a consonant. Explanation: In this example, the vowels are stored in an array after this code initially makes the letter lowercase. We cycle over the array to check if the Character matches any of the vowels. While all three methods reach the same result, they do so by identifying vowels and consonants in letters using various control structures. Choose the one that is the simplest to comprehend and best matches your specific application. By using the Find() Function:Now, the find() method, generally used with strings and holders, is not the immediate choice for this task. In similar cases, distinguishing whether a letter is a vowel or a consonant generally involves relative analysis. To determine if a letter falls into the order of vowels or consonants using the discovery() system, you'd search for it within a string of vowels. The following C++ program demonstrates this approach Code: Output for Various Inputs: Input: 'a' Output: "A is a Vowel." Input: 'B' Output: "B is a Consonant. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India