Problem overview
This is the first version of a small password strength checker. It looks at one thing only: how long the password is. The program prints the number of characters on a line, then a verdict on the next: OK when the password has at least 8 characters, Too short otherwise.
The starter code holds string password = "Secret99";. It has 8 characters, so the output is:
Password length: 8
OK
With abc the output is Password length: 3 followed by Too short.
Approach: measure once, use the number twice
The length is needed in two places: it is printed, and it is compared with 8. So the program stores it in a variable, length, instead of asking the string twice. Then it prints the label and the number, and finishes with an if/else that chooses between two messages. Exactly one of the two messages is printed for every password.
Step by step
- Create
int lengthfrompassword.length(). Astringin C++ is an object, andlength()is a method you call with parentheses. - Print the first line. Chain the pieces with
<<: the text"Password length: ", thenlength, thenendl.coutprints an integer as its digits, so no conversion is needed. - Write the
ifwith the conditionlength >= 8and printOKinside it. - Add the
elseand printToo shortinside it. The closing brace of theifand the wordelsesit on the same line.
The solution in C++
string password = "Secret99";
int length = password.length();
cout << "Password length: " << length << endl;
if (length >= 8) {
cout << "OK" << endl;
} else {
cout << "Too short" << endl;
}
Details worth a closer look:
length()andsize()are the same. Both return the number of characters stored in the string. The lesson useslength()because it reads naturally for text.- The return type is unsigned. The method returns an unsigned integer type, and the code stores it in an
int. That conversion is fine for a password, whose length is far below the range of anint. It matters more when you subtract: for an empty string,password.length() - 1does not give -1, it wraps around to a huge positive number. - Chaining with
<<. Each<<sends one more piece tocout, in order. A string literal, anintandendlcan all appear in the same chain. endlends the line. Without it,OKwould be printed right after the digits on the same line.- The comparison is
>=. At least 8 includes 8, so a password of exactly 8 characters is accepted. With>it would need 9.
What length() counts
For a std::string, length() counts bytes, not visible characters. For plain ASCII text such as letters, digits and punctuation, one byte is one character, and spaces count too: pass word has length 9. For a character outside ASCII, the result can be larger than what you see. The word café, written in UTF-8, has length 5, because the é takes two bytes. The lesson's passwords are ASCII, so the count matches what you read.
Edge cases and common mistakes
- The boundary. A password of 7 characters prints
Too short, and one of 8 printsOK. Check both sides of a boundary whenever you write a comparison. - An empty password.
""has length 0, so the output isPassword length: 0andToo short. - Forgetting the parentheses.
password.lengthwithout()names the method instead of calling it, and the code does not compile. - Using
>instead of>=. It rejects passwords of exactly 8 characters. - Printing the verdict on the same line. Leaving out
endlafter the first message joins the two lines together.
Testing
| password | Output |
|---|---|
| Secret99 | Password length: 8, then OK |
| abc | Password length: 3, then Too short |
| abcdefgh | Password length: 8, then OK |
| abcdefg | Password length: 7, then Too short |
| (empty string) | Password length: 0, then Too short |
The next version builds on this program.
