1 of 58

What Does This Program Do in ACSL? Strings

DR. QIONG CHENG�COMPUTER SCIENCE DEPARTMENT

UNIVERSITY OF NORTH CAROLINA AT CHARLOTTE

1

Copyright © 2021by Qiong Cheng

2 of 58

Motivation

  • Frequently one must use or modify sections of another programmer’s code.
  • It is essential to be able to read and understand an arbitrary program.
  • Code fragments used in this category are examples of “good” BASIC programming.

2

3 of 58

ACSL - What Does This Program Do in ACSL?

  • This category presents a program and asks the student to determine that the program does.

  • The programs are written using a pseudocode that should be readily understandable by all programmers familiar with a high-level programming language, such as Python, Java, or C.

3

4 of 58

Understanding ACSL Pseudo-code

  1. evaluation of arithmetic expressions
  2. single & nested loops (FOR & DO loops)
  3. branching (IF/THEN statements)
  4. subscripted variables
  5. multi-dimensional arrays
  6. string manipulations

4

5 of 58

Constructs Used in the ACSL Pseudo-code

5

6 of 58

Constructs Used in the ACSL Pseudo-code

  • - Operators
  • - Functions
  • - Variables
  • - Sequential statements
  • - Decisional statements

6

7 of 58

Constructs Used in the ACSL Pseudo-code

7

8 of 58

Operators

  • ! (not)
  • ^ or ↑(exponent)
  • *, / (real division)
  • % (modulus)
  • +, -
  • >, <, >=, <=, !=, ==
  • && (and)
  • || (or)

8

in that order of precedence

high

low

9 of 58

Operators

  • ! (not)
  • ^ or ↑(exponent)
  • *, / (real division)
  • % (modulus)
  • +, -
  • >, <, >=, <=, !=, ==
  • && (and)
  • || (or)

9

in that order of precedence

high

low

Expression:

Arithmetic expression: (2 + 3 * 4)/2 + 3^2 + 2 3 + 7 % 3

Logical expression:

- using comparison operators (>, <, >=, <=, !=, ==)

3 < 5, 3!= 5

- using logical operators (!, && (and), || (or))

! (3!=5)

(3 < 5) && (3!=5)

(3 < 5) || (3!=5)

10 of 58

Functions

  • int(x) - greatest integer <= x (returning the next least whole number) Namely, it truncates a number down. This is especially tricky when the argument (the number in the parentheses) is negative. 
  • Examples

  • a. INT(3.2) = 3
  • b. INT(-5.4) = -6
  • c. INT(0) = 0
  • d. INT(-0.4) = -1

10

11 of 58

Functions

  • abs(x) - absolute value (returning the absolute value of the argument)
  • Examples

  • a. ABS(-3) = 3
  • b. ABS(0) = 0
  • c. ABS(14) = 14
  • d. ABS(1236) = 1236
  • e. ABS(-23.45) = 23.45
  • f. ABS(5 * -4) = ABS(-20) = 20

11

12 of 58

Functions

  • sqrt(x) - square root. The SQR (stands for Square Root) function returns the square root of the argument.
  • LEFT(A$, N) - The LEFT (pronounced, "left string") function returns the first N characters (including embedded spaces) from the left end of the string, A$. It requires two arguments. If the value for N is 0, the empty (or null) string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned (with no extra padded spaces).

  • Examples
  •                 a. LEFT("Programming", 3) = "Pro"�                b. LEFT("Basic", 1) = "B"�                c. LEFT("Wyomissing Area", 11) = "Wyomissing "         Note that here is a trailing space in the answer after the 'g'�                d. LEFT("abcd", 0) = ""        (the null string)�                e. LEFT("maryland", 3 - 2) = "m"         You must evaluate the expression, 3-2, before using LEFT�                f. LEFT("high school", 100) = "high school"

12

13 of 58

Functions

  • RIGHT(A$, N) - The RIGHT (pronounced, "right string") function returns the last N characters (including embedded spaces) from the right end of the string, A$. It requires two arguments. If the value for N is 0, the empty string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned. The characters that are returned are not reversed, but rather they keep their original relative order.

  • MID(A$, B, C) - The MID (pronounced, "mid string") function returns the next C characters from the string A$, starting with (and including) the Bth character. It requires three arguments. If the value for C is 0, the empty string is returned. If the value for C exceeds the number of remaining characters after the Bth character, the remaining portion of the string is returned nevertheless with no extra padded spaces.
  • Examples
  •         a. RIGHT("where", 10) = "where“
  • b. RIGHT("United States", 7) = " States“
  • c. MID("Wyomissing", 4, 1) = "m"�        d. MID("high school", 6, 0) = ""    the empty string�         f. MID("abcdef", 5, 24) = "ef"

13

14 of 58

Functions

  • LEN - The LEN (stands for Length) function returns the length of a string. You obtain the length of a string simply by counting the number of characters (including spaces) in that string. The length of the null string is zero.
  • STR - The STR (stands for String and pronounced "string function") function turns a value into a string literal. The final answers to all of the examples below must include double quotes.
  • Examples
  •                 a. LEN("computer") = 8         
  • b. STR(2.789) = "2.789"�            c. STR(0) = "0"      

14

15 of 58

Functions

  • VAL - The VAL (stands for Value) function turns a string literal (that happens to "look like" a numerical value) into a numerical value that could later be added, subtracted, multiplied, etc. in a mathematical expression. Until a string literal such as "15" is turned into a value (like 15, without the quotes), it cannot be manipulated mathematically.
  • SGN - The SGN (stands for Sign) function returns the sign of the argument. That is, it returns -1, if the argument is a negative number. It returns 1 if the argument is a positive number and it returns 0 if the argument is 0, itself. The SGN function returns no other values than 0, 1, and -1.
  • Examples
  •                 a. VAL("-8.9") = -8.9         
  • b. SQR(25) = 5           
  • c. SQR(7) = 2.64575  
  • d. SGN(-23) = -1    
  • e. SGN(0) = 0
  • f. SGN(4.345) = 1

15

16 of 58

Functions

  • VAL - The VAL (stands for Value) function turns a string literal (that happens to "look like" a numerical value) into a numerical value that could later be added, subtracted, multiplied, etc. in a mathematical expression. Until a string literal such as "15" is turned into a value (like 15, without the quotes), it cannot be manipulated mathematically.
  • SGN - The SGN (stands for Sign) function returns the sign of the argument. That is, it returns -1, if the argument is a negative number. It returns 1 if the argument is a positive number and it returns 0 if the argument is 0, itself. The SGN function returns no other values than 0, 1, and -1.
  • Examples
  •                 a. VAL("-8.9") = -8.9         
  • b. SQR(25) = 5           
  • c. SQR(7) = 2.64575  
  • d. SGN(-23) = -1    
  • e. SGN(0) = 0
  • f. SGN(4.345) = 1

16

17 of 58

Statements

  • Sequential statement
  • Decision statements
  • Indefinite Loop statements
  • Definite Loop statements

17

18 of 58

Sequential Statements

  • INPUT variable
  • variable = expression (assignment)
  • OUTPUT variable

  • One commonly used statement in BASIC is the INPUT statement. Presenting a variable name after the keyword INPUT stops the program and allows the user to enter a value for that variable. For example,
  • DIM A AS INTEGER�INPUT A�PRINT A�END

18

Appearing in Contest 1

19 of 58

Decision Statements

  • IF boolean expression THEN
  • Statement(s)
  • ELSE (optional)
  • Statement(s)
  • END IF
  • The user is given the chance to input a value for A. That value is then printed on the screen by the PRINT statement.
  • DIM A, B AS INTEGER�INPUT A, B�IF A > 4 THEN PRINT "A"�IF B > 10 THEN PRINT B�END

19

DIM A AS INTEGERINPUT AIF A > 35 THEN     PRINT (A + 2)ELSE     PRINT (A - 2)END IFEND

DIM A AS INTEGERINPUT AIF A > 35 THEN PRINT (A + 2) ELSE PRINT (A - 2)END

Appearing in Contest 1

20 of 58

Practice

  • When the following program is run, what value is printed?
  •  
  • A = 1: B = 2: C = 3: D = 4
  • If A + C = D then D = D - 2
  • If 2 * D + 4 * A < 10 then A = A + 4
  • If A + D > 3 * B + C then B = C – A
  • If B < C then C = B
  • Print A + B + C + D

20

21 of 58

Practice

  • Print the value of G after the following program is executed.
  •  
  • A = 36: B = 4: C = 5
  • D = A / B + C
  • E = (2 * D + 2) / C
  • IF E > B THEN B = E ELSE E = 3 * B
  • F = A / (B – 1) * C
  • IF F / 2 = INT (F/2) THEN F=F+1 ELSE F = (F-1) / 2
  • G = F * B * E
  • PRINT G

21

22 of 58

Practice

  • What value of D is printed after this program is run?
  • A = 100: B = 10: C = 5: D = 2
  • IF (A / (B * C)) < D THEN A = A/2 ELSE B = B + 2
  • IF (A + 2 * B) >= (C * D) ↑ 2 THEN C = 2 * C ELSE D = D↑2
  • IF (A - C↑2) >= (B * D↑2) THEN A = A/2 ELSE D = D*C
  • IF ((A*B)/C) < (B*C)/D THEN B=B/2 ELSE C= C↑2
  • IF (A>B) OR (C<D) THEN A=A/2 ELSE C=C+4
  • IF (A<C) AND (B>D) THEN B=2*B ELSE D=D+1
  • PRINT D
  • END

22

23 of 58

Practice

  • What are the final values of A, B, C, and D after this program is run?
  • A = 1: B = 2: C = 4: D = 8
  • IF A+D > B*C THEN A = 2 * A ELSE C = C – 2
  • IF A * C – 1 < D + B THEN B = D + 2 ELSE D = B – A
  • IF 3 * C > B – A THEN A = B ELSE C = D
  • IF (A > B) OR (D > C) THEN A = C ELSE B = D
  • IF (A – D > 0) AND (B < C) THEN D = C + 1 ELSE B = A – 2
  • END

23

24 of 58

Constructs Used in the ACSL Pseudo-code

  • - Operators
  • - Functions
  • - Variables
  • - Sequential statements
  • - Decisional statements

24

25 of 58

Loopings

  • Indefinite Loop statements
    • WHILE Boolean expression
    • Statement(s)
    • END WHILE
  • Definite Loop statements
    • FOR variable = start TO end STEP increment
    • Statement(s)
    • NEXT

The FOR loop in BASIC is an example of a repetition statement. It allows a set of statements to continually be executed over and over again for a specific number of iterations. A loop variable is used after the keyword FOR and "lower" and "upper" bounds are specified. On the last line of the loop the keyward NEXT is followed by the loop variable. By default, the loop variable increments by one each time the loop iterates. When the loop variable equals the upper bound it iterates one more time. Then, the loop variable increments but the loop does not iterate and the rest of the program (below/after the loop) executes.

25

DIM A AS INTEGERFOR A = 1 TO 5     PRINT ANEXT AEND

DIM A, B AS INTEGERB = 5FOR A = 1 TO 10     IF A + B >= 12 THEN PRINT "GREATER"NEXT AEND

Appearing in Contest 2

26 of 58

Practice

  • When the following program is run, what is the final value of X?

26

The table shows the values of I, J and X:

I

J

X

1

1

2

1

2

2

3

1

3

2

3

3

4

1

4

2

4

3

4

4

27 of 58

Practice

  • What is the final value of C[4] after the program below is executed?
  • A(1) = 12: A(2) = 41: A(3) = 52
  • A(4) = 57: A(5) = 77: A(6) = -100
  • B(1) = 17: B(2) = 34: B(3) = 81
  • j = 1: k = 1: n = 1
  • WHILE A(j) > 0
  • WHILE B(k) <= A(j)
  • C(n) = B(k)
  • n = n + 1
  • k = k + 1
  • END WHILE
  • C(n) = A(j): n = n + 1: j = j + 1
  • END WHILE

27

Line(s)

j

k

n

A(j)

B(k)

C(n)

28 of 58

Arrays

  • Arrays are variables that hold more than one value of same type.
  • Another word commonly used for arrays is matrix.
  • One way to think about them is as a table like we saw with the adjacency matrix.
  • In ACSL you will normally see arrays that either have one or two dimensions.

  • The number of dimensions is determines how many variables you use to access the array.
  • One dimension arrays are accessed, or indexed, with one index variable.
  • Two dimension arrays are accessed, or indexed, with two index variables.
  • You’ll commonly see arrays paired with loops.

28

29 of 58

Arrays

  • Arrays use ( ) for identifying the subscript(s).
  • The subscript of one-dimensional arrays starts at location 0.
  • For example, A(3) which is the 4th item in a list .

  • Most ACSL past problems start with either A(1) or A(1,1).
  • The size of the array will usually be specified in the problem statement.

29

Appearing in Contest 3

3

5

7

9

11

A(3)

A(0)

0

1

2

3

4

Array A

30 of 58

What Does This Program Do – 1D Arrays

  • Array A initially contains 7,4,2,5,8,3,6,1 in A(0) through A(7).
  • What are the contents of the array after the following program is executed?
  • N= 8 A(0) =
  • FOR J = 0 TO N – 2 A(1) =
  • IF A(J) > A(J+1) THEN A(J) = A(J) + 1 ELSE A(J + 1) = A(J) – 1 A(2) =
  • NEXT J A(3) =
  • FOR K = 1 TO N – 1 A(4) =
  • IF 2 * A(K) = A(K + 1 ) THEN A(K) = 0 A(5) =
  • NEXT K A(6) =
  • END A(7) =

30

7

4

2

5

8

3

6

1

0

1

2

3

4

Array A

5

6

7

J

A

0

1

2

3

4

5

6

31 of 58

What Does This Program Do – 1D Arrays

  • Array A initially contains 7,4,2,5,8,3,6,1 in A(0) through A(7).
  • What are the contents of the array after the following program is executed?
  • N= 8 A(0) =
  • FOR J = 0 TO N – 2 A(1) =
  • IF A(J) > A(J+1) THEN A(J) = A(J) + 1 ELSE A(J + 1) = A(J) – 1 A(2) =
  • NEXT J A(3) =
  • FOR K = 0 TO N – 2 A(4) =
  • IF 2 * A(K) = A(K + 1 ) THEN A(K) = 0 A(5) =
  • NEXT K A(6) =
  • END A(7) =

31

8

5

2

1

0

-1

-2

-3

0

1

2

3

4

Array A

5

6

7

J

A

0

A(0) =8

1

A(1) =5

2

A(3) = 1

3

A(4)=0

4

A(5)=-1

5

A(6)=-2

6

A(7)=-3

K

A

0

1

2

3

4

5

6

32 of 58

What Does This Program Do – 1D Arrays

  • Array A initially contains 7,4,2,5,8,3,6,1 in A(0) through A(7).
  • What are the contents of the array after the following program is executed?
  • N= 8 A(0) =
  • FOR J = 0 TO N – 2 A(1) =
  • IF A(J) > A(J+1) THEN A(J) = A(J) + 1 ELSE A(J + 1) = A(J) – 1 A(2) =
  • NEXT J A(3) =
  • FOR K = 0 TO N – 2 A(4) =
  • IF 2 * A(K) = A(K + 1 ) THEN A(K) = 0 A(5) =
  • NEXT K A(6) =
  • END A(7) =

32

8

5

2

1

0

0

-2

-3

0

1

2

3

4

Array A

5

6

7

J

A

0

A(0) =8

1

A(1) =5

2

A(3) = 1

3

A(4)=0

4

A(5)=-1

5

A(6)=-2

6

A(7)=-3

K

A

0

1

2

3

4

5

A(5)=0

6

33 of 58

Practice

  • After the following program is executed, what is the final value of X?
  • A = “BANAS”
  • X = 0 : T = “”
  • FOR j = len[A] TO 1 STEP –1
  • T = T + A[j]
  • NEXT
  • FOR j = 1 TO len[A]
  • if A[j] = T[j] then X = X+1
  • NEXT

33

The table shows the values of J, A, T, and X:

Line

A

J

T

X

B

A

N

A

S

0

1

2

3

4

Array A

0

1

2

3

4

Array T

34 of 58

  • What is the final value of X after execution?
  • A = “BANAS”
  • X = 0 : T = “”
  • FOR j = len[A] TO 1 STEP –1
  • T = T + A[j]
  • NEXT
  • FOR j = 1 TO len[A]
  • if A[j] = T[j] then X = X+1
  • NEXT

34

A

J

T

X

1

BANANAS

2

.

0

3

7

4

S

3

6

4,5

SA

3

5

4,5

SAN

3

4

4,5

SANA

3

3

4,5

SANAB

6

1

7,8

6

2

7,8

1

6

3

7,8

2

6

4

7,8

3

6

5

B

A

N

A

S

0

1

2

3

4

Array A

S

A

N

A

B

0

1

2

3

4

Array T

1

2

3

4

5

6

7

8

35 of 58

Practice

  • After the following program is executed, what is the final value of X?
  • A = “BANANAS”
  • X = 0 : T = “”
  • FOR j = len[A] TO 1 STEP –1
  • T = T + A[j]
  • NEXT
  • FOR j = 1 TO len[A]
  • if A[j] = T[j] then X = X+1
  • NEXT

35

The table shows the values of J, A, T, and X:

Line

A

J

T

X

B

A

N

A

N

A

S

0

1

2

3

4

Array A

5

6

0

1

2

3

4

Array T

5

6

36 of 58

Practice

  • What is the final value of NUM after the following program is executed?
  • A = "BANANAS"
  • NUM = 0: T = ""
  • for J = len(A) - 1 to 0 step -1
  • T = T + A[J]
  • next
  • for J = 0 to len(A) - 1
  • if A[J] == T[J] then NUM = NUM + 1
  • next

36

The table shows the values of J, A, T, and NUM:

Line

A

J

T

NUM

37 of 58

  • What is the final value of X after execution?
  • A = “BANANAS”
  • X = 0 : T = “”
  • FOR j = len[A] TO 1 STEP –1
  • T = T + A[j]
  • NEXT
  • FOR j = 1 TO len[A]
  • if A[j] = T[j] then X = X+1
  • NEXT

37

Line no.

A

J

T

X

1

BANANAS

2

.

0

3

7

4

S

3

6

4,5

SA

3

5

4,5

SAN

3

4

4,5

SANA

3

3

4,5

SANAN

3

2

4,5

SANANA

3

1

4,5

SANANAB

6

1

7,8

6

2

7,8

1

….

5

B

A

N

A

N

A

S

0

1

2

3

4

Array A

5

6

S

A

N

A

N

A

B

0

1

2

3

4

Array T

5

6

1

2

3

4

5

6

7

8

38 of 58

  • After the following program is executed, what is the final value of C(4)?
  • A(0)=12: A(1)=41: A(2)=52
  • A(3)=57: A(4)=77: A(5)=-100
  • B(0)=17: B(1)=34: B(2)=81
  • j=0: k=0: n=0
  • WHILE A(j) > 0
  • WHILE B(k) <= A(j)
  • C(n) = B(k)
  • n = n+1
  • k = k+1
  • END WHILE
  • C(n) = A(j): n = n+1: j = j+1
  • END WHILE

38

1

2

3

4

5

6

7

8

9

10

11

12

The following table traces the variables through the execution of the program.

12

41

52

57

77

-100

0

1

2

3

4

Array A

5

17

34

81

0

1

2

Array B

Outer Loop

Inner Loop

j

k

n

A(j)

B(k)

C(n)

0

1

2

Array C

3

4

5

6

39 of 58

  • After the following program is executed, what is the final value of C(4)?
  • A(0)=12: A(1)=41: A(2)=52
  • A(3)=57: A(4)=77: A(5)=-100
  • B(0)=17: B(1)=34: B(2)=81
  • j=0: k=0: n=0
  • WHILE A(j) > 0
  • WHILE B(k) <= A(j)
  • C(n) = B(k)
  • n = n+1
  • k = k+1
  • END WHILE
  • C(n) = A(j): n = n+1: j = j+1
  • END WHILE

39

1

2

3

4

5

6

7

8

9

10

11

12

The following table traces the variables through the execution of the program.

12

41

52

57

77

-100

0

1

2

3

4

Array A

5

17

34

81

0

1

2

Array B

Outer Loop

Inner Loop

j

k

n

A(j)

B(k)

C(n)

1

0

0

0

12

17

1

1

41

12

2

1

1

2

34

17

2

2

3

81

34

2

4

52

41

3

3

5

57

52

4

4

6

77

57

5

5

7

-100

77

12

17

34

41

52

57

77

0

1

2

Array C

3

4

5

6

40 of 58

Arrays

  • Two-dimensional arrays use (row, col) order.
  • Arrays can start location (0,0) for two-dimensional arrays.

  • For example, B(2,1) as the cell of row 2 (third row) and column 1 (2nd column).

  • Most ACSL past problems start with A(1,1).
  • The size of the array will usually be specified in the problem statement.

40

Appearing in Contest 3

3

5

7

9

1

3

4

2

1

5

7

9

Row index

Array B

0

1

2

0

1

2

3

B(2, 1)

B(0, 0)

Column index

41 of 58

03-04 C3 What Does this Program Do – 2D Arrays

  • The 3 x 3 array A initially contains the following values: 8,1,6,3,5,7,4,9,2.
  • A(0,0) = 8, A(0,1 ) = 1, …etc.
  • When the following program is executed, what value is printed?
  • X = 0
  • FOR J = 0 TO 2
  • FOR K = 0 TO 2
  • IF A(J,K) / 2 = INT (A(J,K) / 2 )
  • THEN X=X + A(J,K)
  • ELSE X = X – A(J,K)
  • NEXT K
  • NEXT J
  • PRINT X

41

41

8

1

6

3

5

7

4

9

2

Row index

Array A

0

1

2

0

1

2

Column index

Outer Loop

Inner Loop

J

K

A(J,K)

X

42 of 58

03-04 C3 What Does this Program Do – 2D Arrays

  • The 3 x 3 array A initially contains the following values: 8,1,6,3,5,7,4,9,2.
  • A(0,0) = 8, A(0,1 ) = 1, …etc.
  • When the following program is executed, what value is printed?
  • X = 0
  • FOR J = 0 TO 2
  • FOR K = 0 TO 2
  • IF A(J,K) / 2 = INT (A(J,K) / 2 )
  • THEN X=X + A(J,K)
  • ELSE X = X – A(J,K)
  • NEXT K
  • NEXT J
  • PRINT X

42

42

8

1

6

3

5

7

4

9

2

Row index

Array A

0

1

2

0

1

2

Column index

Outer Loop

Inner Loop

J

K

A(J,K)

X

1

1

0

0

8

8

2

1

1

7

3

2

6

13

2

1

1

0

3

10

2

1

5

5

3

2

7

-2

3

1

2

0

4

2

2

1

9

-7

3

2

2

-5

43 of 58

  • What is the final value of C after the program is run?
  • C = 0
  • FOR I = 0 TO 1
  • FOR J = 0 TO 2
  • A(I, J) = 2*I + J
  • IF A(I, J)/2 = INT(A(I, J)/2) THEN A(I, J) = A(I, J)/2 : C = C + 1
  • IF A(I, J)/2 = INT(A(I, J)/2) THEN C = C + 1
  • NEXT J
  • NEXT I

43

I

J

A(I, J)

C

Array A

0

1

2

0

1

2

44 of 58

  • What is the final value of C after the program is run?
  • C = 0
  • FOR I = 1 TO 5
  • FOR J = 1 TO 5
  • A(I, J) = 2*I + J
  • IF A(I, J)/2 = INT(A(I, J)/2) THEN A(I, J) = A(I, J)/2 : C = C + 1
  • IF A(I, J)/2 = INT(A(I, J)/2) THEN C = C + 1
  • NEXT J
  • NEXT I

44

I

J

A(I, J)

C

1

1

3

0

2

4 -> 2

2

3

5

4

6 -> 3

3

5

7

2

1

5

2

6->3

4

3

7

4

8->4

6

5

9

3

1

7

2

8->4

8

3

9

4

10->5

9

5

11

4

1

9

2

10->5

10

3

11

4

12->6

12

5

13

3

4

5

3

7

5

4

7

6

9

7

8

9

9

11

9

10

11

12

13

11

6

13

7

Array A

0

1

2

0

1

2

4

3

3

4

5

5

1

11

2

12->6

14

3

13

4

14->7

15

5

15

5

45 of 58

Arrays in Python

  • A = []
  • A.append(1) #A: [1]
  • A.append(2) #A: [1,2]
  • print(A[0]) # 1
  • Print(A) #[1, 2]
  • Print(*A) #1 2

45

1

2

46 of 58

Two-dimension Arrays in Python: Array of Arrays

  • A = []
  • A.append([1]) #A: [[1]]
  • A.append([2,3]) #A: [[1],[2,3]]
  • A.append([4,5,6]) #A: [[1],[2,3], [4,5,6]]
  • A.append([4,8,9,10]) #A: [[1],[2,3], [4,5,6],[7,8,9,10]]

  • print(A[2,1]) # 5

46

1

2

3

4

5

6

7

8

9

10

[0]

[1]

[2]

[3]

[0]

[1]

[2]

[3]

47 of 58

What Is A String?�

    • A string is a sequence of characters.
    • Strings can contain 0 or more characters and the indexed position starts with 0 at the first character.
    • An empty string has a length of 0.
    • Strings are identified with surrounding double quotes.
    • It can be stored in a variable just like a numeric value.
    • There are functions to convert strings to and from numbers
    • There are functions to manipulate strings just like for numbers
    • String indexes start at 0 (1 (old test)), just like arrays

47

48 of 58

Strings

  • Use [ ] for identifying the characters in a substring of a given string as follows:
    • S = “ACSL WDTPD” (S has a length of 10 and D is at location 9)
    • S[:3] = “ACS” (take the first 3 characters starting on the left)
    • S[4:] = “DTPD” (take the last 4 characters starting on the right)
    • S[2:6] = “SL WD” (take the characters starting at location 2 and ending at location 6)
    • S[0] = “A” (position 0 only).
  • String concatenation is accomplished using the + symbol

  • Errors occur if accessing a character that is in a negative position or greater than the length of the string.

48

Appearing in Contest 4

49 of 58

The LEN function

  • LEN - The LEN (stands for Length) function returns the length of a string. You obtain the length of a string simply by counting the number of characters (including spaces) in that string. The length of the null string is zero.

  • Examples
  •             LEN("computer") = 8�            LEN("a") = 1�            LEN("Wyo Area") = 8   The answer is NOT 7, because the blank space counts as a character.

49

50 of 58

Comparing Strings

  • To compare strings you can use the same operators as numbers: =, , and <>
  • The = and <> operators just check it the strings are equivalent or not.
  • The <, >, <=, >= operators compare on ASCII code value.
  • What do you think this will print if you input the following:
  • ”A” and ”B”?
  • ”A” and ”ABC”?
  • ”ABC” and ”abc”?
  • ”ABC” and ”123”?
  • ”100” and ”2”?

50

51 of 58

Sample Problems

  • After the following program is executed, what is
  • the final value of X?
  • A = “BANANAS”
  • X = 0 : T = “”
  • FOR j = len[A] TO 1 STEP –1
  • T = T + A[j]
  • NEXT
  • FOR j = 1 TO len[A]
  • if A[j] = T[j] then X = X+1
  • NEXT

51

The program first stores the reverse of A$ into T$, and then counts the number of letters that are in the same position in both strings.

Those positions marked with an asterisk contribute one to the value of X. There are 5 such positions.

52 of 58

Sample Problems

  • What is the length of B after this program is run?
  • A = “CINDERELLA” : B= “”
  • FOR I = 0 TO LEN [A] – 1 STEP 2
  • IF A[ I: I] < A[ I + 1: I + 1] THEN B = B + A[ I: I]
  • IF A[I + 1: I + 1] = “L” THEN B = A[I: I] + B
  • IF A[I: I] > “J” THEN B = B + A[I: I] + A[ I: I] + B
  • NEXT I
  • PRINT B

52

I value

B

0

2

4

6

8

C

I

N

D

E

R

E

L

L

A

index

A:

0

1

2

3

4

5

6

7

8

9

53 of 58

Sample Problems

  • What value is printed when the following program is run?
  • X= “”: Y= “”
  • A = “HAPPYEASTER”
  • FOR J = 0 TO LEN [A] − 1
  • IF A[J: J] > A[LEN [A] – J − 1: LEN[A] − J − 1] THEN X = X + A[J: J]
  • NEXT J
  • FOR K = 0 TO LEN [X] − 1
  • IF X[K: K] < “N” THEN Y= Y + X[K: K]
  • NEXT K
  • PRINT Y
  • END

53

J

X

E

A

S

T

E

R

index

A:

0

1

2

3

4

5

K

Y

54 of 58

Sample Problems

  • What value is printed when the following program is run?
  • X= “”: Y= “”
  • A = “HAPPYEASTER”
  • FOR J = 0 TO LEN [A] − 1
  • IF A[J: J] > A[LEN [A] – J − 1: LEN[A] − J − 1] THEN X = X + A[J: J]
  • NEXT J
  • FOR K = 0 TO LEN [X] − 1
  • IF X[K: K] < “N” THEN Y= Y + X[K: K]
  • NEXT K
  • PRINT Y
  • END

54

J

X

0

1

2

3

T

4

TA

5

TAR

E

A

S

T

E

R

index

A:

0

1

2

3

4

5

K

Y

0

T

1

TA

2

55 of 58

The LEFT function

  • LEFT(A$, N) - The LEFT (pronounced, "left string") function returns the first N characters (including embedded spaces) from the left end of the string, A$. It requires two arguments. If the value for N is 0, the empty (or null) string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned (with no extra padded spaces).
  • Examples
  •                 LEFT("Programming", 3) = "Pro"�                LEFT("Basic", 1) = "B"�                LEFT("Wyomissing Area", 11) = "Wyomissing "         Note that here is a trailing space in the answer after the 'g'�                LEFT("abcd", 0) = ""        (the null string)�                LEFT("maryland", 3 - 2) = "m"         You must evaluate the expression, 3-2, before using LEFT�                LEFT("high school", 100) = "high school"

55

56 of 58

The RIGHT function

  • RIGHT(A$, N) - The RIGHT (pronounced, "right string") function returns the last N characters (including embedded spaces) from the right end of the string, A$. It requires two arguments. If the value for N is 0, the empty string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned. The characters that are returned are not reversed, but rather they keep their original relative order.

  • Examples
  •             RIGHT("Programming", 3) = "ing"�            RIGHT("where", 10) = "where"�            RIGHT("United States", 7) = " States"        Note there is a leading space in the answer before the 'S'�            RIGHT("a", 1) = "a"�            RIGHT("abc ", 1) = " "        A blank space

56

57 of 58

The MID function

  • MID(A$, B, C) - The MID (pronounced, "mid string") function returns the next C characters from the string A$, starting with (and including) the Bth character. It requires three arguments. If the value for C is 0, the empty string is returned. If the value for C exceeds the number of remaining characters after the Bth character, the remaining portion of the string is returned nevertheless with no extra padded spaces.
  • Examples:
  •   MID("computer", 4, 2) = "pu"�      MID("computer", 1, 3) = "com"�      MID("Wyo Area", 3, 4) = "o Ar"  Note that the blank space is counted as a character.�      MID("Wyomissing", 4, 1) = "m"�      MID("high school", 6, 0) = ""    the empty string�      MID("abcdef", 5, 24) = "ef"

57

58 of 58

The STR function

  • STR - The STR (stands for String and pronounced "string function") function turns a value into a string literal. The final answers to all of the examples below must include double quotes.

  • Examples
  •             STR(13) = "13"�            STR(2.789) = "2.789"�            STR(0) = "0"�            STR( 4 + 8) = "12"

58