[CS50] 03-2 Problem Set
Published:
In this post, the solution for the problem set of the third lecture of CS50 is summarized.
Problem Set
scrabble
In the game of Scrabble, players create words to score points, and the number of points is the sum of the point values of each letter in the word. For example, if we wanted to score the word “CODE”, we would note that the ‘C’ is worth 3 points, the ‘O’ is worth 1 point, the ‘D’ is worth 2 points, and the ‘E’ is worth 1 point. Summing these, we get that “CODE” is worth 7 points.
Answer
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int scoreCal(string word);
void lowerCase(string word);
int pointTable [] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int main(void){
int player1_score, player2_score;
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
player1_score = scoreCal(word1);
player2_score = scoreCal(word2);
if (player1_score > player2_score){
printf("Player 1 wins!\n");
}
else if (player1_score < player2_score){
printf("Player 2 wins!\n");
}
else{
printf("Tie!\n");
}
}
int scoreCal(string word){
int score = 0;
lowerCase(word);
for (int i = 0, word_len = strlen(word); i < word_len; i++){
if (word[i] >= 'a' && word[i] <= 'z'){
int index = word[i] - 'a';
score += pointTable[index];
}
}
return score;
}
void lowerCase(string word){
for (int i = 0, word_len = strlen(word); i < word_len; i++){
if (word[i] >= 'a' && word[i] <= 'z'){
continue;
}
else if (word[i] >= 'A' && word[i] <= 'Z'){
word[i] += 'a' - 'A';
}
}
}
readability
So what sorts of traits are characteristic of higher reading levels? Well, longer words probably correlate with higher reading levels. Likewise, longer sentences probably correlate with higher reading levels, too.
A number of “readability tests” have been developed over the years that define formulas for computing the reading level of a text. One such readability test is the Coleman-Liau index. The Coleman-Liau index of a text is designed to output that (U.S.) grade level that is needed to understand some text. The formula is
index = 0.0588 * L - 0.296 * S - 15.8
where L is the average number of letters per 100 words in the text, and S is the average number of sentences per 100 words in the text.
Answer
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/*
index = 0.0588 * L - 0.296 * S - 15.8
L = average number of letters per 100 words
S = average number of sentences per 100 words
a word is any sequence of characters separated by a space.
. ! ? indicates a sentence.
*/
int letterCnt(string text);
int wordCnt(string text);
int senCnt(string text);
int main(void){
string text = get_string("Text: ");
int letter_num = letterCnt(text);
int word_num = wordCnt(text);
int sen_num = senCnt(text);
float L = letter_num / (float) word_num * 100;
float S = sen_num / (float) word_num * 100;
int index = round(0.0588 * L - 0.296 * S - 15.8);
if (index < 1){
printf("Before Grade 1\n");
}
else if(index <= 15){
printf("Grade %i\n", index);
}
else{
printf("Grade 16+\n");
}
}
int letterCnt(string text){
int cnt = 0;
for (int i = 0, word_len = strlen(text); i < word_len; i++){
if (isalpha(text[i])){
cnt++;
}
}
return cnt;
}
int wordCnt(string text){
int cnt = 0;
for (int i = 0, word_len = strlen(text); i < word_len; i++){
if (isblank(text[i])){
cnt++;
}
}
cnt++;
return cnt;
}
int senCnt(string text){
int cnt = 0;
for (int i = 0, word_len = strlen(text); i < word_len; i++){
if (text[i] == 33 || text[i] == 46 || text[i] == 63){
cnt++;
}
}
return cnt;
}
substitution
In a substitution cipher, we “encrypt” (i.e., conceal in a reversible way) a message by replacing every letter with another letter. To do so, we use a key: in this case, a mapping of each of the letters of the alphabet to the letter it should correspond to when we encrypt it. To “decrypt” the message, the receiver of the message would need to know the key, so that they can reverse the process: translating the encrypt text (generally called ciphertext) back into the original message (generally called plaintext).
A key, for example, might be the string NQXPOMAFTRHLZGECYJIUWSKDVB. This 26-character key means that A (the first letter of the alphabet) should be converted into N (the first character of the key), B (the second letter of the alphabet) should be converted into Q (the second character of the key), and so forth.
A message like HELLO, then, would be encrypted as FOLLE, replacing each of the letters according to the mapping determined by the key.
Answer
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int alphaCheck(string key);
int repCheck(string key);
int repCheck(string key);
void cipher(string plaintext, string key);
int main(int argc, string argv []){
if (argc != 2){
printf("Usage: ./substitution key\n");
return 1;
}
else{
if(strlen(argv[1]) != 26){
printf("Key must contain 26 characters.\n");
return 1;
}
else if(alphaCheck(argv[1]) == 0){
printf("Key must contain 26 characters.\n");
return 1;
}
else if(repCheck(argv[1]) == 0){
printf("Key must contain 26 characters.\n");
return 1;
}
else{
string plaintext = get_string("plaintext: ");
cipher(plaintext, argv[1]);
printf("ciphertext: %s\n", plaintext);
return 0;
}
}
}
int alphaCheck(string key){
for (int i = 0, key_len = strlen(key); i < key_len; i++){
if (isalpha(key[i]) == 0){
return 0;
}
}
return 1;
}
int repCheck(string key){
int subArr [26];
for (int i = 0; i < 26; i++){
subArr[i] = 0;
}
for (int i = 0, key_len = strlen(key); i < key_len; i++){
if (islower(key[i])){
subArr[key[i] - 'a'] = 1;
}
else if (isupper(key[i])){
subArr[key[i] - 'A'] = 1;
}
}
for (int i = 0, key_len = strlen(key); i < key_len; i++){
if (subArr[i] == 0){
return 0;
}
}
return 1;
}
void cipher(string plaintext, string key){
for (int i = 0, text_len = strlen(plaintext); i < text_len; i++){
if (islower(plaintext[i]) && islower(key[plaintext[i] - 'a'])){
plaintext[i] = key[plaintext[i] - 'a'];
}
else if(islower(plaintext[i]) && isupper(key[plaintext[i] - 'a'])){
plaintext[i] = key[plaintext[i] - 'a'] - ('A' - 'a');
}
else if(isupper(plaintext[i]) && islower(key[plaintext[i] - 'A'])){
plaintext[i] = key[plaintext[i] - 'A'] + ('A' - 'a');
}
else if(isupper(plaintext[i]) && isupper(key[plaintext[i] - 'A'])){
plaintext[i] = key[plaintext[i] - 'A'];
}
else{
plaintext[i] = plaintext[i];
}
}
}

Leave a Comment