CS50 Problem Set 2

CS50 Problem Set 2

King Hua Lv2

CS50 Problem Set 2

Here’s my answer for the CS50 Problem Set 2. Hope that will help you a bit.

Problem 1: Scrabble

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int compute_score(string word);

int points[] = {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)
{
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");

int score1 = compute_score(word1);
int score2 = compute_score(word2);

if (score1 > score2)
{
printf("Player 1 wins!\n");
return 0;
}
else if (score1 < score2)
{
printf("Player 2 wins!\n");
return 0;
}
else
{
printf("Tie!\n");
return 0;
}
}

int compute_score(string word)
{
int score = 0;

for (int i = 0; i < strlen(word); i++)
{
if (isupper(word[i]))
{
score += points[word[i] - 'A'];
}
else if (islower(word[i]))
{
score += points[word[i] - 'a'];
}
}
return score;
}

Problem 2: Readability

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int letters(string text);
int sentences(string text);
int words(string text);

int main(void)
{
string text = get_string("Text: ");

int index = (int) round(0.0588 * letters(text) / words(text) * 100 -
0.296 * sentences(text) / words(text) * 100 - 15.8);

if (index < 1)
{
printf("Before Grade 1\n");
return 0;
}
else if (index >= 16)
{
printf("Grade 16+\n");
return 0;
}
else
{
printf("Grade %i\n", index);
return 0;
}
}

int words(string text)
{
int words = 1;
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == ' ')
{
words += 1;
}
}
return words;
}

int letters(string text)
{
int letters = 0;
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
letters += 1;
}
}
return letters;
}

int sentences(string text)
{
int sentences = 0;
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences += 1;
}
}
return sentences;
}

Problem 3: Substitution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])
{
string key = argv[1];
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
{
for (int i = 0, n = strlen(key); i < n; i++)
{
if (isalpha(argv[1][i]) == 0)
{
printf("Key should be all alphabets.\n");
return 1;
}

for (int j = i + 1; j < n; j++)
{
if (argv[1][i] == argv[1][j] || argv[1][i] + 32 == argv[1][j] ||
argv[1][i] - 32 == argv[1][j])
{
printf("There should be no duplicate in the key.\n");
return 1;
}
}
}
string ptext = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, length = strlen(ptext); i < length; i++)
{
if (isupper(ptext[i]))
{
char result = toupper(key[ptext[i] - 'A']);
printf("%c", result);
}
else if (islower(ptext[i]))
{
char result = tolower(key[ptext[i] - 'a']);
printf("%c", result);
}
else
{
printf("%c", ptext[i]);
}
}
printf("\n");
return 0;
}
}
  • Title: CS50 Problem Set 2
  • Author: King Hua
  • Created at : 2026-02-16 20:13:38
  • Updated at : 2026-02-16 21:02:03
  • Link: https://kinghua0629.github.io/2026/02/16/CS50-Problem-Set-2/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments