Checking if character entered is a vowel – switch-case – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1973 siteicon   siteicon   siteicon  

Checking if character entered is a vowel – switch-case

Entering a character from console and then checking if entered character is a vowel or something else.

Learning Objectives

  • Learning to use switch-case construct for character data type.
  • Learning a large number of fall through cases for writing them in any form (one-line, multiple line, one in each line etc.

Source Code

TC++ #1973

 

Source Code

Run Output

Enter an alphabet(a-z/A-Z) : A
It is a vowel

-OR-
Enter an alphabet(a-z/A-Z) : t
It is not a vowel

Code Understanding

switch (c) {
Here we are checking for a character type data in the switch

case ‘A’: case ‘a’: case ‘E’: case ‘e’: case ‘I’: case ‘i’: case ‘O’: case ‘o’: case ‘U’: case ‘u’:
cout <<“It is a vowel”; break;
Since this is a check for character type data so each data element will be written in case within single quotes such as case ‘A’: etc. Multiple case can be written in a sequence so as to implement the fall through method of switch case. After the last case is checked the instructions can be written which is followed by a break instruction.

default: cout <<“It is not a vowel”;
This one is the default case, which means that none of the case specified has matched, so the message is accordingly printed. The break instruction is not required here as it is already the last case.

Notes

  • The case <value>: syntax uses the formatting of c++ which allows any number of spaces between the different entities. The most common writing formats are -Typical fall through style:
    case ‘A’:
    case ‘a’:
    case ‘E:
    case ‘e’:  and so on..

    Multiple line style:
    case ‘A’: case ‘a’:
    case ‘E: case ‘e’:  and so on..

    Single line style:
    case ‘A’: case ‘a’: case ‘E: case ‘e’:  and so on..


Suggested Filename(s): vowel-check.cpp



Share

sunmitra| Created: 28-Dec-2017 | Updated: 15-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back