Week 2 - Arrays
Last updated
Last updated
Welcome to CS50 Week 2! As normal, I will be going through my summary of Week 1's content.
It is not case-sensitive, which means for example 'c' and 'C' carry the same point.
Every letter that is not alpha carries 0 point.
Compute the score of a string
In the snippet, we can learn the style of traversing through a string in C, that's for (int i, len = strlen(word); i < l; i++)
, then we can use word[i]
to represent each letter in the word in the loop.
To compute the reading level of a text, we will use Coleman-Liau index, whose formula is index = 0.0588 * L - 0.296 * S - 15.8
, where L
is the average number of lettes per 100 words, and S
is the average number of sentences per 100 words in the text.
Notice that this formula may output "wrongly" if you only input one word, like hello
, in this case, what you will get is Grade 14
, since sentence
is 0. However, if we add a termination signal at the end, we will get the reasonable output. This is one disadvantage of this formula.
In count_letters()
, we only need to count the characters that are alphabetical, so isalpha()
will be useful.
In count_words()
, we may assume that a sentence:
will contain at least one word;
will not start or end with a space; and
will not have multiple spaces in a row; and
will not start with !
, .
or ?
So, based on these assumptions, we'll consider any sequence of characters seperated by a space to be a word.
In count_sentences()
, we only need to consider any sequence of characters that ends with a .
or a !
or a ?
to be a sentence.
count_sentences()
To round a result (usually in float or double) to the nearest whole number, we can use the round()
declared in math.h
.
Caesar's algorithm encrypts messages by "rotating" each letter by positions.
The program should only accept only a single command-line argument, a non-negative integer. Otherwise, the program should output Usage: ./caesar key
and return form main
a value of 1
.
The program must preserve case: capitalized letters, though rotated, must remain capitalized letters; lowercase letters, though rotated, must remain lowercase letters.
The command-line argument, int argc, string argv[]
template
Check whehter the input is a non-negative integer or not.
3. Rotate each alphabetical letter
The get_string()
provided in the <cs50.h>
won't truncate the extra white space behind. For example, if you input 123
, the extra white space behind 3
will also be counted into the string. But in this problem, because of the use of string argv[]
, it will use white space to seperate between strings, so the extra white space won't be counted to argv[1]
, and it will only contain 123\0
.
Every character in the key must be alphabetical, case-sensitive and appear only once, which means c
and C
can not appear in the key at the same time.
Validate the key
method.
method
Encrypt the alphabetical character
In the validation part, the idea of keep tracking is important and will decrease the time compelxity tremendously.
The idea of index in the letter and array problem is important also. If it is case-sensitive in the problem, we can use toupper()
or tolower()
to map the letter to its index.