Introduction to Probability

Multivariable Calculus

Algorithms: Part II

Algorithms: Part I

Introduction to Software Design and Architecture

Calculus Two: Sequences and Series

LAFF Linear Algebra

Stanford Machine Learning

Calculus One

Computational Thinking

Effective Thinking Through Mathematics

CS50 Introduction to Computer Science

Others

Week 2

Cryptography

String

  • a sequence of characters, in an array (a list of things right next to each other) in memory.

  • Sample with string.h:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    // ask user for input
    string s = get_string();

    // make sure get_string returned a string
    if (s != NULL)
    {
    // iterate over the characters in s one at a time
    for (int i = 0, n = strlen(s); i < n; i++)
    {
    // print i'th character in s
    printf("%c\n", s[i]);
    }
    }
    }

Typecasting

  • ASCII is a standrad for mapping characters to letters. Here are some sample ones:

    1
    2
    3
    4
    5
    A   B   C   D   E   F   G   H   I  ...
    65 66 67 68 69 70 71 72 73 ...

    a b c d e f g h i ...
    97 98 99 100 101 102 103 104 105 ...
    • We can experiment with this program:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      #include <stdio.h>

      int main(void)
      {
      // treat numbers like characters:
      for (int i = 65; i < 65 + 26; i++)
      {
      printf("%c is %i\n", (char) i, i);
      }

      // we can also treat characters like numbers:
      for (char c = 'A'; c <= 'Z'; c++)
      {
      printf("%c is %i\n", c, c);
      }
      }
    • toupper in <ctype.h>, implements:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      #include <cs50.h>
      #include <stdio.h>
      #include <string.h>

      int main(void)
      {
      string s = get_string();
      if (s != NULL)
      {
      for (int i = 0, n = strlen(s); i < n; i++)
      {
      if (s[i] >= 'a' && s[i] <= 'z')
      {
      // ('a' - 'A') = 32
      printf("%c", s[i] - ('a' - 'A'));
      }
      else
      {
      printf("%c", s[i]);
      }
      }
      printf("\n");
      }
      }

String in Memory

  • strlen:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <cs50.h>
    #include <stdio.h>

    int main(void)
    {
    string s = get_string();
    int n = 0;
    while (s[n] != '\0') # \0 is end of the string, NOT space
    {
    n++;
    }
    printf("%i\n", n);
    }
  • a string in C is just the location of the first character in memory, which are stored with a character at the end marking the end of a string, since there’s no predetermined length, so a string in memory really looks like:

    1
    2
    3
    ------------------------------
    | Z | a | m | y | l | a | \0 |
    ------------------------------
    • And with \0, C indicates the end of our string.
  • We can represent more of our computer’s memory as a grid:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    -----------------------------------
    | Z | a | m | y | l | a | \0 | A |
    -----------------------------------
    | n | d | i | \0 | | | | |
    -----------------------------------
    | | | | | | | | |
    -----------------------------------
    | | | | | | | | |
    -----------------------------------
    • We can imagine each byte (each box in this grid) of memory as labeled from 0 to 31, since there are 32 bytes total. In the sample, Zamyla start with 0, and Andi start with 7.

Command-Line Arguments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cs50.h>
#include <stdio.h>

int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else
{
printf("hello, world\n");
}
}
1
2
~/workspace/ $ ./argv0 hello
hello, hello
  • argc : argument count
  • argv : argument vector, a list of strings
  • argv[0] is always the name of the program itself.

main's output

  • main return a number 0 to indicate a program exists successfully. A non-zero number is used to present an error.:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <cs50.h>
    #include <stdio.h>

    int main(int argc, string argv[])
    {
    if (argc != 2)
    {
    printf("missing command-line argument\n");
    return 1;
    }
    printf("hello, %s\n", argv[1]);
    return 0;
    }
  • We can use command $? to see the exit code in terminal, like this:

    1
    2
    3
    4
    ~/workspace/ $ ./exit
    missing command-line argument
    ~/workspace/ $ echo $?
    1

Refers