Cs50 Problem set 2 Solutions 2020: My step by step explanation

After submitting pset 1, here comes another scary problem set’s that took me hours before completion, though I thought I was going to skip it at first.

While problem 2 seems to be a tough task, I’ve complete the solutions and have done a well-explained solution in this post.

Let’s dive in.

What to do in Pset2?

We are to submit this solution using Cs50 ide

  • Submit Readability

Then Submit one of:

  • Caesar if feeling less comfortable
  • Substitution if feeling more comfortable

Cs50 Readability Problem set 2 Solution

What we are told to do in this problem set is to write a C program that computes/takes text from the user and determines it’s reading level i.e like Grade 1, 2, 3, and so on.

Pseudocode

  • First Prompt user for text to check
  • Iterate through the length of the text to count
    • the number of letter in the text
    • the number of words
    • the number of sentences
  • Then implement the index calculation and round it up
  • Then print the Grade of the Text
Check the code’s //comment for better explanation

Cs50 Caesar Problem set 2 Solution

In this Pset, we are told to write a program that asks for a Certain key through the Command line argument to encipher a Plaintext that’s being input by the user.

Pseudocode

  • Check that program was run with one command-line argument
  • Iterate over the provided argument to make sure all characters are digits
  • Convert that command-line argument from a string to an int
  • Prompt user for plaintext
  • Iterate over each character of the plaintext:
    • If it is an uppercase letter, rotate it, preserving case, then print out the rotated character
    • If it is a lowercase letter, rotate it, preserving case, then print out the rotated character
    • If it is neither, print out the character as is
  • Print a newline

Cs50 Substitution Problem set 2 Solution

This Pset is designed to write a program that takes plaintext from the user and encipher it using substitution technique. The user will run the code with 26 alphabetic characters on the command line.

Pseudocode

  • Check that program was run with one command line with exactly all 26 alphabet without space
  • Iterate through the length of the second command line (Argument_Vector) to
  • Make sure command line contain only 26 alphabet
  • make sure it doesn’t contain repeated alphabet
  • Then ask user for plaintext to encipher
  • Then iterate through the plaintext and substitute it with the correct alphabet…. thus preserving the plaintext case (uppercase or lower)
  • Then if not alphabet, print the plaintext like that.
  • Print new line
pls check the //comment in the code to understand every line of code

pls note: you can check all code comment (starting with //) to understand more about what every line is doing, I’ve prepared a well-composed comment to make you understand better.

Conclusion

This Problem set is interesting, it makes me learn more about using the command line argument, also makes me understand more about ASCII code, along with using already documented header (“like Ctype.h, String.h, Stdlib.h”) to make code organized.

While the code might help you, always make sure you understand every line of code, because it will be like a background for coming weeks Pset.

for any questions you might have, kindly use the comments box, I will get back to you Asap.

Thanks for reading….

2 comments

  1. Hi! So far your articles have helped me a lot to understand better the problems sets and their lessons. I just have one question. Why are you setting the return value to 1 at the beginning? I’m still trying to understand how return values work but I don’t get how do you use it there.

    1. Return actually helps to check if a program run successfully, return 1 means true while 0 means false

Leave a Reply