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 1: Check Character Case (Upper or Lower)

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

Aim

To read a single character from the console and classify it as an uppercase letter, a lowercase letter, or a non-alphabetic character using the char type and the Unicode-aware System.Char API.

Theory

char in C# is a 16-bit value type (System.Char) that stores a UTF-16 code unit — not a 1-byte ASCII code as in C. Because char is a struct living on the stack (or inline inside its container), classifying characters never allocates on the GC heap. Classification methods such as char.IsLetter, char.IsUpper and char.IsLower consult the Unicode category tables (Lu = Letter uppercase, Ll = Letter lowercase), so they work for 'É' as correctly as for 'A' — a naive range test like ch >= 'A' && ch <= 'Z' fails for accented letters. Console.ReadLine() returns a string (an immutable reference type); indexing it with input[0] yields a char at zero conversion cost because a string IS a read-only sequence of UTF-16 code units. The program also demonstrates the ternary conditional operator and the defensive guard string.IsNullOrWhiteSpace — protection against a null or empty line (in online runners stdin is often empty, hence the default 'A'). Note that Console.ReadLine returns null at end-of-stream, which is exactly the case the guard covers.

Requirements

  • .NET SDK 8.0+ and any editor, or the built-in RUN CODE button
  • CLI commands: dotnet new console, dotnet run

Procedure

  1. Create the project: dotnet new console -n CaseCheck, then cd CaseCheck.
  2. Paste the program into Program.cs.
  3. Run with dotnet run, type a character (e.g. g) and press Enter.
  4. Re-run with G, 9 and an empty line to exercise all four code paths (upper, lower, non-letter, default).

Explanation of the Code

  • string input = Console.ReadLine(); reads one whole line; it may be null when the input stream is closed.
  • char ch = string.IsNullOrWhiteSpace(input) ? 'A' : input[0]; — the ternary picks the safe default 'A' when no input is supplied, otherwise takes the first UTF-16 code unit of the line.
  • if (!char.IsLetter(ch)) rejects digits, punctuation and whitespace BEFORE the case test — order matters: testing IsUpper first would silently misreport '9' as "lowercase" in the else branch.
  • char.IsUpper(ch) checks Unicode category Lu; the final else therefore means lowercase letter.
  • $"{ch} is Uppercase." is string interpolation: the compiler lowers it to string.Format (or the allocation-free DefaultInterpolatedStringHandler in .NET 6+).

Expected Output

Interactive session (typed input echoes after the prompt):

Enter a character (default A): g
g is Lowercase.

With input GG is Uppercase.; with 7Not an alphabet character.; with empty input the default prints A is Uppercase.

🎯 Viva Questions

  1. Is char a value or reference type? — Value type (System.Char), 16 bits wide, stored inline or on the stack.
  2. Why prefer char.IsUpper over ch >= 'A' && ch <= 'Z'? — It is Unicode-aware and correct for non-ASCII letters such as 'É'.
  3. What does Console.ReadLine return at end-of-stream?null, which is why the null/whitespace guard exists.
  4. What does string interpolation compile to?string.Format or an interpolated string handler; {ch} is a compile-time format hole, not a runtime text splice.
  5. What is the Unicode category of 'A'? — Lu (Letter, uppercase).
  6. Why check IsLetter before IsUpper? — To route non-letters to their own branch; otherwise digits would fall into the lowercase else.