Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 2: Check Whether Character is Vowel

Lesson 3 of 14 in the free C# Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To accept a character from the keyboard and decide whether it is a vowel, demonstrating case normalisation with char.ToLower and short-circuit boolean composition.

Theory

A vowel test is a set-membership problem. The professional solution normalises case FIRST (char.ToLower) so a single comparison chain covers both 'E' and 'e' — halving the number of conditions and removing a whole class of copy-paste bugs. The chain ch == 'a' || ch == 'e' || ... uses the short-circuit OR operator: evaluation stops at the first operand that is true, so on average fewer than three comparisons execute. Character literals in C# are genuine char values compared by their UTF-16 code units — an O(1) integer comparison with no string machinery involved. Alternatives worth knowing for the viva: "aeiou".Contains(ch) (readable but a linear scan through a string), a C# 9 pattern ch is 'a' or 'e' or 'i' or 'o' or 'u' (compiled to an efficient compare/jump sequence), and HashSet<char> for large alphabets. Finally, char.ToLower(ch) is culture-sensitive; char.ToLowerInvariant avoids the famous Turkish-I problem ('I' lowercases to dotless 'ı' under the tr-TR culture) — a real internationalisation bug class in shipped software.

Requirements

  • .NET SDK 8.0+ (dotnet new console, dotnet run) or the RUN CODE button.

Procedure

  1. Create the project: dotnet new console -n VowelCheck, then cd VowelCheck.
  2. Replace Program.cs with the given code.
  3. Execute dotnet run and type a character such as E.
  4. Test the boundary cases: uppercase vowel, consonant, digit, and empty input (defaults to 'e').

Explanation of the Code

  • char.ToLower(string.IsNullOrWhiteSpace(input) ? 'e' : input[0]) — one expression performs three jobs: null guard, default selection, and case normalisation. After this line only lowercase letters need testing.
  • bool isVowel = ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; — the result of the boolean expression is stored directly into a bool; no if statement is needed to "manufacture" a boolean value.
  • Console.WriteLine(isVowel ? $"{ch} is a vowel." : $"{ch} is not a vowel."); — the ternary selects between two interpolated strings; only the chosen string is ever constructed at run time.

Expected Output

Interactive session:

Enter a character (default e): K
k is not a vowel.

Note the echo is lowercase because normalisation happened before printing. With empty input the default path prints e is a vowel.; with O it prints o is a vowel.

🎯 Viva Questions

  1. Why normalise case before comparing? — One comparison set handles both cases; fewer branches, fewer bugs.
  2. What is short-circuit evaluation?|| stops at the first true operand; && stops at the first false.
  3. Difference between char.ToLower and char.ToLowerInvariant? — The first honours the current culture (Turkish-I pitfall); the second uses invariant rules and is deterministic.
  4. How would a C# 9 pattern express this test?ch is 'a' or 'e' or 'i' or 'o' or 'u'.
  5. Is 'a' a string? — No; single quotes create a char literal (one UTF-16 code unit), double quotes create a string.
  6. Complexity of the comparison chain? — O(1): at most five integer comparisons, fewer on average due to short-circuiting.