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
- Store the length. Declare an
intnamedlengthand set it topassword.Length. - Print the first line. Join the label and the number with
+and pass the result toConsole.WriteLine. - Write the
if. Its condition islength >= 8. Inside its braces, printOK. - Add the
else. It runs for every case theifdid not take, so it needs no condition of its own. Inside it, printToo 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:
Lengthhas no parentheses. On a string it is a property, not a method. Writingpassword.Length()does not compile: the compiler reports that the non-invocable memberstring.Lengthcannot be used like a method. Spelling itpassword.lengthfails too, because C# is case-sensitive and the compiler finds no member calledlengthonstring.+joins text and numbers. When one side of+is astringand the other is anint, C# converts the number to its text form and joins the two."Password length: " + lengthis the textPassword length: 8.Console.WriteLineends 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, soif (length)is a compile error. The comparisonlength >= 8produces abool. - Braces and layout. The condition goes in parentheses and each block in braces. Here
elseshares a line with the closing brace of theifblock. 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"printsOK. With>instead of>=it would printToo short. - Exactly 7 characters.
"abcdefg"printsPassword length: 7andToo short. - An empty password.
""has a length of 0 and printsToo short. Nothing breaks, because an empty string is still a string. - A password that is
null. Astringvariable can holdnull, which is different from an empty string, and reading.Lengthon it throws aNullReferenceException. 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 wordlength. The variable must sit outside the quotes, joined with+.
Testing
"Secret99"givesPassword length: 8andOK."abc"givesPassword length: 3andToo short."abcdefg"givesPassword length: 7andToo short.""givesPassword length: 0andToo short.
