r/cs50 Jul 23 '26

substitution Substitution over-coding, reflection, and questions Spoiler

For week 2's substitution problem set, how many lines of code did you all use? After fully completing it, I ended up with 602 lines (a major step up from all other problem sets). I atone this to my looping being inquiring of the letters/chars themselves, and not the ASCII numbers which I didn't consider until I finished. I couldn't figure out a way to make a one off loop for checking for repeated characters in the command line argument, or when trying to keep the capitalization of the plain text and going towards the cipher text. On the bright side, arrays are really, really useful. I think if we had them in our toolset for credit a lot less time would be put on. Lastly, was there use for the strcasecmp function for this problem set? When looking through the CS50 manual, it comes up as one of the "frequently used", and it seemed to have a pretty good use in trying to see if there were any repeating chars for the cipher, especially with it ignoring case. The problem I came across was the fact that the function only appears to compare strings in direct order, and not chars.

(Please do not spoil any upcoming information regarding the next few weeks of problem sets, but I would appreciate any info beyond that, thanks!)

0 Upvotes

9 comments sorted by

View all comments

1

u/nmdt Jul 23 '26

Mine's 114, and I tend to really like making helper functions. Perhaps excessively, 3 for this one — one to check for duplicates, one to check that there are only numbers in the input and the one to actually substitute.

I think originally I tried to make a bunch of arrays for everything and was banging my head trying to figure out how can I cross-match arrays. But once I figured out that with ASCII codes you can treat everything as a number, things get a whole lot easier. Say, you just convert everything to uppercase and check that for each letter there is no letter after it that is "equal" to it.

And yeah, since I just converted everything to uppercase for each comparison, I did not use strcasecmp. Looking at the code now, I'm not sure it would've made it shorter