Hey guys! First post and I am really excited to share my solution to the practice problem “Password” from W2 of CS50!! I was really struggling in W1 with loops and now even with W2 with bool expressions and arrays but I am really proud to say that I found this solution all on my own! Every problem so far I’ve needed a tutorial (or even just retyping someone else’s code till it clicked), so its nice to see all my hardworking taking effect
// Practice iterating through a string
// Practice using the ctype library
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
bool valid(string password);
int main(void)
{
string password = get_string("Enter your password: ");
if (valid(password))
{
printf("Your password is valid!\n");
}
else
{
printf("Your password needs at least one uppercase letter, lowercase letter, number and symbol\n");
}
}
// TODO: Complete the Boolean function below
bool valid(string password)
{
bool upper = false;
bool lower = false;
bool num = false;
bool sym = false;
int pass_length = strlen(password);
for (int i = 0; i < pass_length; i++) {
if(islower(password[i])) {
lower = true;
}
}
for (int i = 0; i < pass_length; i++) {
if (isupper(password[i])) {
upper = true;
}
}
for (int i = 0; i < pass_length; i++) {
if (isdigit(password[i])) {
num = true;
}
}
for (int i = 0; i < pass_length; i++) {
if (ispunct(password[i])) {
sym = true;
}
}
if (sym == true && num == true && upper == true && lower == true) {
return true;
}
else
return false;
}
type or paste code here