Password
Learning Goals
- Practice iterating through a string
- Practice using the
ctypelibrary - Practice using Boolean variables
背景
If this视频 is blocked in your country, don’t worry, as it 不是必需的 to solve the problem. It’s a humorous clip 关于 passwords from the movie Spaceballs.
As we all know by now, it’s important to use passwords that are not easy to guess! Many web apps now require passwords that require not only alphabetical characters, but also number and symbols.
In this 实验, the user is prompted for a password, which will then be validated using a function check that you will complete. If the password contains at least one upper case letter, one lower case letter, a number, and a symbol (意思ing a printable character that’s not a letter or number, such as ‘!’, ‘$’ and ‘#’), the function should return true. If not it should return false.
提示
- The
ctypelibrary has many useful functions that can come in handy here. - Boolean variables can keep track of whether each criteria in an algorithm is valid.
演示

入门
- Log into cs50.dev using your GitHub account.
- Click inside the terminal window and execute
cd. - At the
$prompt, typemkdir password - Now execute
cd password - Then copy and paste
wget https://cdn.cs50.net/2022/fall/实验s/2/password.cinto your terminal to 下载 this 实验’s 分发代码. - You are to complete the function,
valid, which returnstrueif the password passes all criteria, andfalseif it does not.
实现细节
Your function will iterate through the password that’s supplied to it as an argument. Since you have 查找 at least one lower case letter, one upper case letter, one number and one symbol, you可能想要 to 创建一个 boolean variable for each and set each to false before you iterate through the string. If you then find a number, for instance you can set that boolean to true. If all booleans are true at the end of the function, it 意思s all criteria are met, and you would return true.
Thought 问题
- How many different passwords do you think can be made that are 8 letters long, can use any of 95 printable ASCII characters?
如何测试 Your Code
你的程序应该 behave per the例子below.
password/ $ ./password
Enter your password: hello
Your password needs at least one uppercase letter, lowercase letter, number and symbol!
password/ $ ./password
Enter your password: h3ll(
Your password needs at least one uppercase letter, lowercase letter, number and symbol!
password/ $ ./password
Enter your password: h3LL0!
Your password is valid!
You can check your code using check50, a program that CS50 will use to test your code when you 提交, by typing in the following at the $ prompt. But be sure to test it自己 as well!
check50 cs50/实验s/2023/x/password
To evaluate that the style of your code, type in the following at the $ prompt.
style50 password.c
如何提交
No need to 提交! This is an 可选 练习题 completed with your 实验.