Start learning free

LESSON 194 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in C#: A Length Check with if/else

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

string password = "Secret99";

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

password.Length is a property (no parentheses).

string password = "Secret99";

int length = password.Length;
Console.WriteLine("Password length: " + length);
if (length >= 8) {
    Console.WriteLine("OK");
} else {
    Console.WriteLine("Too short");
}

STEP 1 OF 4
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in C#, explained

    Problem overview

    This is the first of a series of password checkers. The program starts with a string named password and reports on it in two lines. The first line is Password length: followed by the number of characters. The second line is a verdict: OK if the password has at least 8 characters, Too short otherwise.

    For "Secret99" the output is:

    Password length: 8
    OK
    

    Eight characters meet the "at least 8" rule, so the verdict is OK. For "abc" the length is 3 and the verdict is Too short.

    Approach: measure once, then decide

    The program needs the length twice: once to print it and once to compare it with 8. Storing it in an int variable means the length is computed in one place and used in both. The verdict is a two-way choice, which is what if/else is for: run one block when the condition is true and the other block when it is false.

    The rule says "at least 8", so the boundary value 8 belongs to the passing side. The comparison is therefore length >= 8, not length > 8. Everything from 0 to 7 characters takes the else branch.

    Step by step

    1. Store the length. Declare an int named length and set it to password.Length.
    2. Print the first line. Join the label and the number with + and pass the result to Console.WriteLine.
    3. Write the if. Its condition is length >= 8. Inside its braces, print OK.
    4. Add the else. It runs for every case the if did not take, so it needs no condition of its own. Inside it, print Too short.

    The solution in C#

            string password = "Secret99";
    
            int length = password.Length;
            Console.WriteLine("Password length: " + length);
            if (length >= 8) {
                Console.WriteLine("OK");
            } else {
                Console.WriteLine("Too short");
            }
    

    The C# details worth a closer look:

    • Length has no parentheses. On a string it is a property, not a method. Writing password.Length() does not compile: the compiler reports that the non-invocable member string.Length cannot be used like a method. Spelling it password.length fails too, because C# is case-sensitive and the compiler finds no member called length on string.
    • + joins text and numbers. When one side of + is a string and the other is an int, C# converts the number to its text form and joins the two. "Password length: " + length is the text Password length: 8.
    • Console.WriteLine ends the line. Each call prints its text and then moves to a new line, so the two calls produce two output lines.
    • The condition must be a bool. C# does not treat numbers or strings as true or false, so if (length) is a compile error. The comparison length >= 8 produces a bool.
    • Braces and layout. The condition goes in parentheses and each block in braces. Here else shares a line with the closing brace of the if block. That is a layout choice only; the compiler ignores line breaks and indentation.

    What Length counts

    A C# string is a sequence of UTF-16 code units, and Length counts those units. For letters, digits and ordinary punctuation, one character is one unit, so the count matches what you see. A few symbols outside the basic range, such as an emoji, take two units. The password "a😀" has a Length of 3 even though it looks like two characters. For the passwords in this lesson that difference doesn't arise.

    Edge cases and common mistakes

    • Exactly 8 characters. "abcdefgh" prints OK. With > instead of >= it would print Too short.
    • Exactly 7 characters. "abcdefg" prints Password length: 7 and Too short.
    • An empty password. "" has a length of 0 and prints Too short. Nothing breaks, because an empty string is still a string.
    • A password that is null. A string variable can hold null, which is different from an empty string, and reading .Length on it throws a NullReferenceException. The lesson's password is always a real text, so there is nothing to guard against here.
    • Spaces count. A space is a character like any other, so "ab cdefg" has length 8.
    • Printing the number inside the quotes. "Password length: length" prints the word length. The variable must sit outside the quotes, joined with +.

    Testing

    • "Secret99" gives Password length: 8 and OK.
    • "abc" gives Password length: 3 and Too short.
    • "abcdefg" gives Password length: 7 and Too short.
    • "" gives Password length: 0 and Too short.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free