CS50 Week 2 C — Scrabble Scores, Readability Grades and Substitution Ciphers
- Parsa Dev
- Jan 25
- 2 min read

Week 2 of CS50x moves into arrays, strings, and command-line arguments — and the three problem sets for this week are a proper step up from Week 1. Scrabble is the warm-up: two players each enter a word, the program scores each one using the official Scrabble letter values stored in an integer array, and the winner is declared. The scoring logic works by finding each letter's position in the alphabet — using character arithmetic like word[i] - 'a' to get the index — and looking up its point value. Upper and lower case are both handled, and non-alphabetic characters score zero. It is a clean little program that makes arrays feel genuinely useful rather than theoretical. Readability is more interesting. It takes a block of text, counts the letters, words, and sentences, and runs the Coleman-Liau index formula to estimate the US grade level needed to read it. Word counting is done by counting spaces and the null terminator, sentence counting looks for full stops, exclamation marks, and question marks, and the whole thing is broken into four separate functions to keep the logic tidy. Feeding in a paragraph and watching it spit out "Grade 7" or "Grade 16+" is surprisingly satisfying.
Substitution is where Week 2 gets genuinely challenging. The program takes a 26-character key as a command-line argument — each letter of the key maps to a corresponding letter of the alphabet — and uses it to encrypt a plaintext message entered by the user. Before any encryption happens, the key goes through three separate validation checks: it must be exactly 26 characters long, it must contain only alphabetic characters, and it must not contain any repeated letters. Each check is its own bool function, which keeps the validation logic separate from the main program flow and makes the code much easier to read. The ciphering function itself iterates through the plaintext, matches each letter to its position in the alphabet using two reference strings — one uppercase, one lowercase — looks up the corresponding key character, and preserves the original case of the input. Memory is allocated with malloc for the output string, and the null terminator is set manually at the end. It is one of those problems where getting the logic right requires you to hold several moving parts in your head at once.
What Week 2 drives home is how much you can do with arrays and strings in C once you understand how they actually work under the hood. Character arithmetic, manual memory allocation, command-line argument parsing with argc and argv — none of this is complicated once it clicks, but it takes working through real problems like these to make it click. The advantage of building things like the Coleman-Liau readability index from scratch, rather than calling a library function, is that you end up understanding the maths behind it, not just the result. The full code for this week is up on GitHub — you can browse the Week 2 folder directly, or explore the entire CS50x repository to follow along week by week.




Comments