Examining Assignment [EA2a]

Your task is to write a program that lets a user select a password. The program has to make sure that the password is "safe" by requiring a set of properties of the password. In order to be regarded safe all of the following must be true:

  • The password must contain at least eight characters.
  • The password must contain at least one number.
  • The password must contain both uppercase letters and lowercase letters.

The program first asks the user for a password and then controls whether the given password is safe. If not, the program should clearly state all the reasons why the password is not safe, and give the user another chance to enter a password until a safe password is entered.

Once a safe password has been entered, the program should ask the user to confirm the password by entering it again. If the new password is identical to the previous one, the program finally accepts the password and quits, otherwise the user have to start over again. The following is an example run (your program does not have to be identical):

Example run (the users input is in bold):

Enter a password: abc
The password is too short. Please enter at least 8 characters.
The password does not contain any digits.
The password does not contain mixed case.
Enter a password: a1b2c3
The password is too short. Please enter at least 8 characters.
The password does not contain mixed case.
Enter a password: A1b2c345
This is a valid password. Confirm by entering it again:
Confirm password: abc12345
This password differs from the one you entered. Password not confirmed.
Enter a password: ABc12345
This is a valid password. Confirm by entering it again:
Confirm password: ABc12345
Password ABc12345 is confirmed. Good bye!

Implementation and Rules

This is a multistep assignment, only read and make one assignment at a time. Don't move to the next one until the previous one is completed. In step 1 and 2 we will create the functions that determines whether a password is safe or not, and in step 3 we will put it together so that it looks like in the example run above.

You may not use global variables, static variables, the exit() function or the goto-statement in any assignment. If you don't know what these things are, then you dont have to worry.

Step 1: Making the rules

Each of the safety rules should be controlled independently by three different functions:

  • passwordIsLongEnough()
  • passwordContainsDigit()
  • passwordHasMixedCase()

Each of these functions should take a password (i.e., a text string) as argument and return a value: either 0 or 1. They should return 1 if and only if the stated rule is true for the password that has been sent. In other words, passwordContainsDigit(str) should return the value 1 if the string str contains a digit and the value 0 if not.

Tip 1: study the functions in Figure 8.1 of the course book.

Rule: These functions may only return a value. They are not allowed to take any input from the user or print anything on the screen (in other words: they may not call print() or scanf() ).

Tip 2: try to design each of these functions on paper before you start to code. Make sure you have a plan before starting to implement the function. if you did all the exercises, you already have working implementations of some of these functions.

Tip 3: Before you move on to the next function, make sure your function behaves as intended by testing it. It will be much easier to find errors later if you have made sure your function acts as intended. Test it by calling it from main() with some hard coded strings.

Step 2: Checking the password

The next step is to implement a function that checks a password for correctness by calling each of the three functions from the previous step. The function signature should look like this:

int isSafePassword(char password[])

This function should return the value 1 if all of the three functions from step 1 return the value 1 and 0 otherwise. The function should also print an appropriate message depending on if the password is safe or not. If the password is safe, the function should print something along the lines of "this is a valid password". If not, the function should print all the reasons that it is not.

In print a reason why a password is not safe, you need to check the return values from the functions listed in step 1 and do appropriate printouts. It is advised to first draw a flow chart so you know how you should structure your code.

Rules:

  • isSafePassword() must call all of the three functions in step 1
  • You may not change anything in the three functions from step 1. You may not add any text output for example, all printouts should be made from the function body of isSafePassword().

Step 3: Putting it together

The final step is to make the main() function. In main() you should repeatedly ask the user for a valid password until it is determined to be safe. When a safe password has been entered, the user should be asked to confirm the password (as in the example run). .

Tips:

  • Once again the functions listed in ch 8.3 may be useful to compare the entered password with the confirmed password.
  • Remember to use flowcharts/pseudocode so that you know what you will code before you start with the main()-function.

Rules:

  • The enterPassword() function must call isSafePassword() but may not call any of the functions in step 1 directly.

Examining Assignment [EA2b]

This assignment builds directly on [EA2a]. Use the same project and everything, there's no need to keep [EA2a] in isolation.

Your task is now to extend the program so that three different users should select passwords. Your functionality from [EA2a] should check each password in the same way. The program should then print all the users and their passwords on the screen

Example run:

Enter a username: Alice
Enter a password: a1b2c3
The password is too short. Please enter at least 8 characters.
The password does not contain mixed case.
Enter a password: ABc12345
This is a valid password. Confirm by entering it again:
Confirm password: ABc12345
Password ABc12345 is now associated with user: Alice.
…(same for user 2)
Enter a username: Bob
Enter a password: XYd12345
This is a valid password. Confirm by entering it again:
Confirm password: XYd12345
Password XYd12345 is associated with user: Bob.
Here are all users and passwords:
User Password
Alice ABc12345

Bob XYd12345

Implementation and Rules

We will do this by defining a new type that associates a username with a password (a struct).

Step 1: define a struct

The first thing you need to do is to define a new type (struct) called struct account. This struct should contain the name of a user and the users password. Both members should be strings of appropriate length. Do this definition at the top of the file, before any functions. Tip: use #define for the length of these strings.

Step 2: An array of accounts

Add an array of three accounts (the type you just created) in your main()-function:

struct account userAccounts[3];

At the end of the program, this array should be filled with three user accounts (username + password).

Modify your main()-function so that it looks like in the example run. You can skip the final step (printing the users and passwords) until step 3.

Rules:

The program must contain a loop that iterates over the 3 user accounts and at the end of each iteration, userAccounts should have set the password and username for one user

Step 3: Show a list of user accounts

Finally, you should display a list of user accounts as the example run shows. You do this by making a loop that iterates over all user accounts and prints them. Note that the user names and passwords must be fetched from the userAccounts-array.

Tip: use \\t in your printf() to make tabs and make the table look nice.

Examining Assignment [EA2c]

This is the last part of [EA2] and just adds the possibility for to specify how many users and passwords that will be needed. Continue to build this from your [EA2a-b] project. Here's an example run:

How many users and passwords? 4
Enter a username: Alice
Enter a password: ABc12345
This is a valid password. Confirm by entering it again:
Confirm password: ABc12345
Password ABc12345 is associated with user: Alice.
… (continued 3 times)

Rules and Implementation

The idea here is that the user should be asked for amount of user accounts. Instead of having an array of three users, you should replace this with a dynamic array and allocate the number of user accounts indicated by the user.

This is a quite small modification of the original program since dynamical arrays works just as static ones. Remember to free the allocated memory at the end of the program1.

Even though in this case it is not strictly necessary. All allocated memory will be freed at the end of the program. But in general there should be a free()-call for every malloc() and this shows that you have thought about that.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.