**scrabble.c** Code:
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
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 score_calc(string answer);
int main(void)
{
string player_answer_one = get_string("Player 1: ");
string player_answer_two = get_string("Player 2: ");
int score_player_one= score_calc(player_answer_one);
int score_player_two = score_calc(player_answer_two);
if (score_player_one > score_player_two)
{
printf("Player 1 wins\n");
}
else if (score_player_two > score_player_one)
{
printf("Player 2 wins\n");
}
else
{
printf("Tie\n");
}
}
int score_calc(string answer)
{
int score = 0;
for (int i=0, length = strlen(answer); i<length; i++)
{
if (isupper(answer[i]))
{
score = score + points[answer[i] - 'A'];
}
else if (islower(answer[i]))
{
score = score + points[answer[i] - 'a'];
}
else
{
}
}
return score;
}
**readability.c** Code:
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// user input
string text = get_string("Text: ");
int len = strlen(text);
float letters = 0;
float sentences = 0;
float words = 0;
for (int i = 0; i < len; i++)
{
if (isalpha(text[i]))
{
letters = letters + 1;
}
else if (isblank(text[i]))
{
words = words + 1;
}
else if (text[i] == 46 || text[i] == 33 ||
text[i] == '?') //(ispunct(text[i]) && text[i]!=',')
{
sentences = sentences + 1;
// printf("added, %c\n", text[i]);
/* if (text[i]==39 || text[i]==34 || text[i]==':')
{
sentences = sentences - 1;
printf("subtracted, %c\n", text[i]);
*/
}
else
{
continue;
}
}
words += 1;
/*printf("letters count: %f\n", letters);
printf("words count: %f\n", words);
printf("sentences count: %f\n", sentences);
//JUST FOR DEBUG HERE*/
float L = (letters / words) * 100;
float S = (sentences / words) * 100;
// printf("L is: %f\nS is: %f\n", L, S);
float index = 0.0588 * L - 0.296 * S - 15.8;
index = round(index);
// output
if (index < 1)
{
printf("Before Grade 1\n");
return 0;
}
else if (index > 16)
{
printf("Grade 16+\n");
return 0;
}
printf("Grade %i\n", (int) index);
/*
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.
*/
}
caesar.c Code:
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
// agruments count shpuldnt be below or above 2
if (argc < 2 || argc > 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
string text = argv[1];
for (int i = 0, len = strlen(text); i < len; i++)
{
// isdigit(text[i]);
if (!isdigit(text[i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
// convert text(string) into k (integer)
int key = atoi(text);
string plaintext = get_string("plaintext: ");
int sum;
printf("ciphertext: ");
for (int i = 0, len = strlen(plaintext); i < len; i++)
{
// blanks wont apply:
if (isalpha(plaintext[i]))
{
sum = key + plaintext[i];
if (isupper(plaintext[i]))
{
while (sum > 90)
{
sum = sum - 26;
}
}
else if (islower(plaintext[i]))
{
while (sum > 122)
{
sum = sum - 26;
}
}
printf("%c", sum);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
return 0;
}
**substitution.c** the hardest task on Problem Set 2:
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc > 2 || argc < 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
string key = argv[1]; //"0: B 1: C ...DEFGHIJKLMNOPQRSTUVWXYZA"
int keylength = strlen(key);
const int LEN = 26;
for (int i = 0; i < LEN; i++)
{
if (!isalpha(key[i]))
{
printf("Usage: ./substitution key\n");
return 1;
}
}
if (keylength != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
// current key, does it exists again? yes? return erorr (1). no? you fine cointune
int next = 0;
for (int current = 0; current < LEN; current++)
{
next = current + 1;
while (next < LEN)
{
// printf("compare: %c [current: %i] = %c [next: %i]\n", key[current],current,
// key[next], next);
if (key[current] - key[next] == 0 || tolower(key[current]) - key[next] == 0)
{
// printf("found duplicate: %c [current] = %c [next]\n", key[current], key[next]);
return 1;
}
next += 1;
}
}
// user input
string plaintext = get_string("plaintext: "); // A: 65 B.66 ... CDEFGHIKLMNOPQRSTUVWXYZ
// key = ZYX
// input: CAB
// output: XZY -> 1. go look into the key. 2. change letter based on key.- 1.C is X in the key
// set C as X
int iteration = strlen(plaintext);
printf("ciphertext: ");
for (int i = 0; i < iteration; i++)
{
if (!isalpha(plaintext[i]))
{
printf("%c", plaintext[i]);
}
else
{
if (isupper(plaintext[i]))
{
printf("%c", toupper(key[plaintext[i] - 65])); // A (65) - C (67) = 2 - > postion of
// the key array -> key[2]
}
else if (islower(plaintext[i]))
{
printf("%c", tolower(key[plaintext[i] - 97]));
}
}
}
printf("\n");
return 0;
}