38
0 Welcome! COMP1511 18s1 Programming Fundamentals

Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

0Welcome!COMP1511 18s1

Programming Fundamentals

Page 2: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

1COMP1511 18s1 — Lecture 7 —

StringsAndrew Bennett

<[email protected]>

chars arrays of chars

strings

Page 3: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

Before we begin… 

introduce yourself to the person sitting next to you

 

why did they decide to study computing?

Page 4: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

3

Overviewafter this lecture, you should be able to…

understand the basics of chars

understand what ASCII is

understand the basics of strings

write programs using strings to solve simple problems

(note: you shouldn’t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember

programming is like learning any other language, it takes consistent and regular practice.)

Page 5: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

4

AdminDon’t panic!

course style guide published

week 4 weekly test due friday don’t be scared!

assignment 1 out now work on it regularly!

additional autotests added to the assignment

don’t forget about help sessions! see course website for details

Page 6: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

5

Beyond Numberswe’ve mostly seen numbers thus far

int age = 18; double pi = 3.14

what else might we want to store?

Page 7: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

6

Letters and Wordswhat about words?

printf("andrew is awesome");

Page 8: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

7

Letters and Wordswhat about words?

printf("andrew is awesome");

“andrew is awesome”

Page 9: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

8

Letters and Wordswords in C are called strings

printf("andrew is awesome");

“andrew is awesome”

^ this is a string

Page 10: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

9

introducing: strings

Page 11: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

10

Stringsa string is an array of characters.

note: a character is a “printed or written letter or symbol”.

Page 12: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

11

Charactersa character generally refers to a letter, number, punctuation, etc.

in C we call it a char

Page 13: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

12

Characters in Cin C we call it a char

// making an int

int age = 18;

// making a char

char letter = 'A';

char s go inside single quotes, i.e. ' . strings go inside double quotes, i.e. " .

Page 14: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

13

Characters in Cchar stores small integers.

8 bits (almost always).

mostly used to store ASCII character codes

don’t use for individual variables, only arrays

only use char for characters

(not to store e.g. numbers between 0-9)

Page 15: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

14

ASCIIASCII is a way of mapping numbers to characters.

it contains:

upper and lower case English letters: A-Z and a-z

digits: 0-9

common punctuation symbols

special non-printing characters: e.g newline and space.

Page 16: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

15

ASCIIyou don’t have to memorize ASCII codes!

single quotes give you the ASCII code for a character:

printf("%d", 'a'); // prints 97

printf("%d", 'A'); // prints 65

printf("%d", '0'); // prints 48

printf("%d", ' ' + '\n'); // prints 42 (32 + 10)

don’t put ASCII codes in your program - use single quotes instead!

Page 17: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

16

ASCIIlet’s try it out!

Page 18: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

17

Reading charsgetchar()

reads a byte from standard input returns an int

returns a special value if it can’t read a byte otherwise returns an integer (0..255)

(ASCII code)

let’s try it out!

Page 19: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

18

getcharconsider the following code:

int c1, c2;

printf("Please enter first character:\n");

c1 = getchar();

printf("Please enter second character:\n");

c2 = getchar();

printf("First %d\nSecond: %d\n", c1, c2);

what should this do?

what does it actually do?

(how can we fix it?)

Page 20: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

19

getchar

int c1, c2;

printf("Please enter first character:\n");

c1 = getchar();

printf("Please enter second character:\n");

c2 = getchar();

printf("First %d\nSecond: %d\n", c1, c2);

what should this do? read two typed characters

what does it actually do? read one typed character + enter

Page 21: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

20

getcharhow can we fix it?

int c1, c2;

printf("Please enter first character:\n");

c1 = getchar();

getchar(); // extra getchar to catch the newline

printf("Please enter second character:\n");

c2 = getchar();

printf("First %d\nSecond: %d\n", c1, c2);

Page 22: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

21

End Of Inputscanf or getchar will fail if there isn’t any more input

eg if you’re reading from a file and reach the end of the file

getchar returns a special value to indicate no more input is available

we call this value EOF

(how could you check this with scanf?)

Page 23: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

22

Reading until End of Input

int c;

c = getchar();

while (c != EOF) {

printf("’%c’ read, ASCII code is %d\n", c, c);

c = getchar();

}

reading numbers until end of input with scanf:

int num;

// scanf returns the number of items read

while (scanf("%d", &num) == 1) {

printf("you entered the number: %d\n", num);

}

Page 24: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

23

Stringsstrings are an array of characters

Page 25: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

24

Remember Arrays?A series of boxes with a common type,

all next to each other

0 1 2 3 4 5 6 7 8 9 10 11

╔════╤════╤════╤════╦════╤════╤════╤════╦════╤════╤════╤════╦┄

║ │ │ │ ║ │ │ │ ║ │ │ │ ║

╚════╧════╧════╧════╩════╧════╧════╧════╩════╧════╧════╧════╩┄

Page 26: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

25

Arrays in C

// Declare an array with 10 elements

// and initialises all elements to 0.

int myArray[10] = {0};

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 27: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

26

Arrays in C

int myArray[10] = {0};

// Put some values into the array.

myArray[0] = 3;

myArray[5] = 17;

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 3 │ 0 │ 0 │ 0 │ 0 │ 17 │ 0 │ 0 │ 0 │ 0 │

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 28: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

27

Character Arrayswe can make an array of chars in the same way

char myArray[10] = {0};

// Put some values into the array.

myArray[0] = 65;

myArray[5] = 70;

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐

│ 65 │ 0 │ 0 │ 0 │ 0 │ 70 │ 0 │ 0 │ 0 │ 0 │

└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 29: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

28

How long is a piece of string?you don’t always know the length of a string in advance

e.g. name could be “Andrew”, or “Tom” (6 characters vs 3 characters)

// "Andrew" is 6 letters long

name[0] = 'A';

name[1] = 'n';

name[2] = 'd';

name[3] = 'r';

name[4] = 'e';

name[5] = 'w';

// "Tom" is 3 letters long

name[0] = 'T';

name[1] = 'o';

name[2] = 'm';

Page 30: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

29

How long is a piece of string?we need a way to know how long the string is!

name[0] = 'A'; name[1] = 'n'; name[2] = 'd';

name[3] = 'r'; name[4] = 'e'; name[5] = 'w';

┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐

│ A │ N │ D │ R │ E │ W │ │ │ │ │

└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

(please never write code on one line like this! it’s only here so the slides fit)

Page 31: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

30

How long is a piece of string?we need a way to know how long the string is!

name[0] = 'T'; name[1] = 'o'; name[2] = 'm';

┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐

│'T'│'O'│'M'│ R │ E │ W │ │ │ │ │

└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

printing name would print TOMREW

Page 32: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

31

Null Terminatorwe do this in C using a null terminator

any function (e.g. printf) working with a string interprets this as “end of string”.

name[0] = 'T';

name[1] = 'o';

name[2] = 'm';

name[3] = '\0';

Page 33: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

32

Null Terminator

┌───┬───┬───┬────┬───┬───┬───┬───┬───┬───┐

│'T'│'O'│'M'│'\0'│ E │ W │ │ │ │ │

└───┴───┴───┴────┴───┴───┴───┴───┴───┴───┘

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

printing name would now print TOM

Page 34: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

33

Sidenote: Uninitialised Arrayswhat happens if we don’t initialise our array?

let’s try it and see!

Page 35: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

34

Sidenote: Uninitialised Arrayswhat’s wrong with this code?

int array[SIZE];

int i = 0;

while (i < SIZE) {

printf("%d\n", array[i]);

i++;

}

Page 36: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

35

Sidenote: Uninitialised Arraysthe array has not been initialised

int array[SIZE];

int i = 0;

while (i < SIZE) {

printf("%d\n", array[i]);

i++;

}

┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐

│ ? │ ? │ ? │ ? │ ? │ ? │ ? │ ? │ ? │ ? │

└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

Page 37: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

36

Sidenote: Uninitialised Arrayssolution: initialise the array first

(note: you could also initialise all the values in a loop)

int array[SIZE] = {0};

int i = 0;

while (i < SIZE) {

printf("%d\n", array[i]);

i++;

}

┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐

│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │

└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

Page 38: Welcome! 0 [webcms3.cse.unsw.edu.au] · 2018-03-27 · 3 Over view a f t e r t h i s l e c t u r e , y o u s h o u l d b e a b l e to … understand the basics of chars understand

37

Sidenote: Uninitialised Arraysdcc can catch this for you if you tell it to use valgrind

dcc -o blah blah.c --valgrind