Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson8 / 20

    Module 2 · Input, loops and methods: make programs respond · Lesson 8 of 20

    Turn typed text into a valid task title

    Should a task containing only spaces be added to the planner? Separate missing input, blank input and a useful title before you use the text.

    Predict with the supplied input

    Run this complete program and enter two spaces, Read C#, then two spaces before pressing Enter. In this example, the input line is Read C# . The typed characters are input; they are not extra lines printed by the program.

    C#
    using System;
    
    Console.WriteLine("Task title:");
    string? raw = Console.ReadLine();
    
    if (string.IsNullOrWhiteSpace(raw))
    {
        Console.WriteLine("Enter a title.");
    }
    else
    {
        string title = raw.Trim();
        Console.WriteLine($"Saved: [{title}]");
    }

    What does the program print after Task title: for the supplied input?

    A Saved: [ Read C# ]
    B Saved: [Read C#]
    C Enter a title.
    D Saved: [ReadC#]
    Show answer

    Correct answer: B

    Trim removes the leading and trailing spaces. The space inside Read C# remains.

    Option A: The assigned result comes from Trim, which removes the outer spaces.

    Option C: The input contains non-whitespace characters.

    Option D: Trim does not remove the space between the words.

    Hint: Separate outer spaces from the space inside the title.

    Draw the validation gate

    Text
    ReadLine
       │
       ├── null, empty, or only whitespace ──> ask for a title
       │
       └── contains text ──> Trim outer whitespace ──> use cleaned title

    string? means the variable may hold a string or null. Null means no string value is present. An empty string has a value, but contains zero characters. Pressing Enter on a blank line gives an empty string; reaching the end of the input stream can give null. The program should handle both.

    string.IsNullOrWhiteSpace checks for null, an empty string, or a string containing only whitespace. It gives a bool result, so it fits naturally inside if. The accepted branch runs only when some non-whitespace text exists. After that check, the compiler also understands that raw is not null in the else branch.

    For this task, reject blank input first, then clean it. Do not call a method such as Trim on a possibly null string and hope that a user always types something.

    Which value makes IsNullOrWhiteSpace return True?

    A Read C#
    B Read C#
    C A string containing only spaces
    D C# 10
    Show answer

    Correct answer: C

    A string containing only whitespace has no title text, so the check returns True.

    Option A: There are visible non-whitespace characters.

    Option B: Outer spaces do not erase the text between them.

    Option D: Letters and digits are title text.

    Hint: Ask whether any non-whitespace text exists.

    Cleaning returns another value

    Strings do not change their character contents in place. Keep the result of Trim if you want the cleaned text. The brackets below make otherwise invisible outer spaces visible.

    C#
    using System;
    
    string original = "  Read C#  ";
    string cleaned = original.Trim();
    
    Console.WriteLine($"[{original}]");
    Console.WriteLine($"[{cleaned}]");

    After cleaned = original.Trim(), which statement is correct?

    A original is now null
    B Both variables necessarily contain empty strings
    C original lost its outer spaces
    D cleaned has trimmed text; original keeps its original text
    Show answer

    Correct answer: D

    The returned trimmed value is stored in cleaned. This does not edit the original string contents.

    Option A: Nothing assigns null to original.

    Option B: The input contains Read C#.

    Option C: Trim returns a result; it does not mutate the existing string.

    Try it independently

    Add a rule: the cleaned title must contain at least three characters. Count after trimming, so surrounding spaces cannot satisfy the rule. Length is the number of UTF-16 code units in a string; for the simple English titles here, it matches the character count you see. More complex text can need different handling.

    Test C# : expect the short-title message. Test Read : expect Saved: [Read]. Compare only after your attempt.

    C#
    using System;
    
    Console.WriteLine("Task title:");
    string? raw = Console.ReadLine();
    
    if (string.IsNullOrWhiteSpace(raw))
    {
        Console.WriteLine("Enter a title.");
    }
    else
    {
        string title = raw.Trim();
        if (title.Length < 3)
        {
            Console.WriteLine("Use at least 3 characters.");
        }
        else
        {
            Console.WriteLine($"Saved: [{title}]");
        }
    }

    Explain back

    Explain the difference between null, empty text and spaces. Point to the line that prevents a null value from reaching Trim. Finally explain why the minimum-length rule uses the cleaned value.

    References: Microsoft — Console.ReadLine and IsNullOrWhiteSpace.

    Quick reference

    • Console.ReadLine can return null when input ends.
    • string? allows a nullable string value.
    • IsNullOrWhiteSpace handles null, empty and whitespace-only text.
    • Trim returns text with outer whitespace removed.
    • Keep the returned value and validate the cleaned title.
    • Test blank, short, exact-boundary and valid inputs.
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·