What Does This Program Do? - Strings

Contest 4 adds string manipulation to WDTPD problems. You’ll trace programs that slice, search, concatenate, and transform strings. The main challenge is keeping track of indices - strings are zero-indexed in ACSL, just like arrays.

ACSL string operations

ACSL pseudocode uses these string functions. Memorize them - they appear in almost every problem.

Length

len("HELLO") = 5

Returns the number of characters.

Indexing (zero-based)

S = "COMPUTER"
S[0] = "C"
S[3] = "P"
S[7] = "R"

Watch out: The last valid index is , not .

Substring

S = "COMPUTER"
S[2:5] = "MPU"

S[start:end] returns characters from index start up to but not including end. This is the same convention as Python slicing.

  • S[0:3] = “COM” (indices 0, 1, 2)
  • S[5:] = “TER” (index 5 to end)
  • S[:4] = “COMP” (start to index 3)

Concatenation

"HELLO" + " " + "WORLD" = "HELLO WORLD"

The + operator joins strings end to end.

Find / Index

S = "BANANA"
find(S, "AN") = 1

Returns the index of the first occurrence of the substring. Returns -1 if not found.

ASCII values

ACSL problems sometimes use and :

Key ranges:

  • Uppercase A-Z: -
  • Lowercase a-z: -
  • Digits 0-9: -

Useful trick: gives the position of an uppercase letter ().

Try it: trace a string program

Watch how the string variable changes as operations are applied.

Code
1S = "HELLO"2S = REVERSE(S)3x = LEN(S)4c = S[0]5OUTPUT: c, x
Variables
NameValue
S"HELLO"
Step 1 of 5

String tracing strategy

For string problems, keep a string state table showing the value of each string variable after every operation.

Example:

S = "PROGRAMMING"
T = ""
FOR i = 0 TO len(S) - 1
    IF S[i] == "G" OR S[i] == "M" THEN
        T = T + S[i]
    END IF
NEXT
OUTPUT: T

Walk through each character:

  • P: no
  • R: no
  • O: no
  • G: yes, T = “G”
  • R: no
  • A: no
  • M: yes, T = “GM”
  • M: yes, T = “GMM”
  • I: no
  • N: no
  • G: yes, T = “GMMG”

Answer: GMMG

Speed tip: For filtering problems like this, just scan the string for matching characters instead of tracing every iteration.

Common string patterns in ACSL

Pattern 1: Reversing a string

S = "ACSL"
R = ""
FOR i = len(S) - 1 TO 0 STEP -1
    R = R + S[i]
NEXT
OUTPUT: R

Builds the string backwards: L, then LS, then LSC, then LSCA.

Answer: LSCA

Pattern 2: Character replacement

S = "HELLO WORLD"
T = ""
FOR i = 0 TO len(S) - 1
    IF S[i] == " " THEN
        T = T + "-"
    ELSE
        T = T + S[i]
    END IF
NEXT
OUTPUT: T

Answer: HELLO-WORLD

Pattern 3: Counting characters

S = "MISSISSIPPI"
count = 0
FOR i = 0 TO len(S) - 1
    IF S[i] == "S" THEN
        count = count + 1
    END IF
NEXT
OUTPUT: count

Answer: 4 (S appears at indices )

Pattern 4: Case conversion using ASCII

S = "Hello"
T = ""
FOR i = 0 TO len(S) - 1
    c = ord(S[i])
    IF c >= 97 AND c <= 122 THEN
        T = T + chr(c - 32)
    ELSE
        T = T + S[i]
    END IF
NEXT
OUTPUT: T

This converts lowercase to uppercase. The difference between ‘a’ () and ‘A’ () is .

Answer: HELLO

Pattern 5: Palindrome check

S = "RACECAR"
is_palindrome = true
FOR i = 0 TO INT(len(S) / 2) - 1
    IF S[i] != S[len(S) - 1 - i] THEN
        is_palindrome = false
    END IF
NEXT
OUTPUT: is_palindrome

Compares: R=R, A=A, C=C. Only needs to check characters. All match.

Answer: true

Pattern 6: Building a string by index mapping

S = "COMPUTER"
idx = [3, 0, 7, 4, 1, 5, 2, 6]
T = ""
FOR i = 0 TO 7
    T = T + S[idx[i]]
NEXT
OUTPUT: T
T
PP
CPC
RPCR
UPCRU
OPCRUO
TPCRUOT
MPCRUOTM
EPCRUOTME

Answer: PCRUOTME

This is an anagram/permutation of the original string.

Pattern 7: Extracting words

S = "THE QUICK FOX"
words = 1
FOR i = 0 TO len(S) - 1
    IF S[i] == " " THEN
        words = words + 1
    END IF
NEXT
OUTPUT: words

Answer: 3 (two spaces = three words)

String + array combinations

Contest 4 problems often combine strings and arrays. A common pattern is treating a string as an array of characters.

Example: Caesar cipher

S = "HELLO"
shift = 3
T = ""
FOR i = 0 TO len(S) - 1
    c = ord(S[i]) - ord("A")
    c = (c + shift) MOD 26
    T = T + chr(c + ord("A"))
NEXT
OUTPUT: T
(position)T
HKK
EHKH
LOKHO
LOKHOO
ORKHOOR

Answer: KHOOR

The handles wrap-around (Z + 1 = A).

Common mistakes

  1. Off-by-one with zero indexing. S = "CAT" has characters at indices . S[3] is out of bounds.
  2. Substring end index is exclusive. S[1:3] gives characters (at indices and ), not .
  3. Forgetting string immutability. In most pseudocode, you build a new string - you can’t change a character in place. S[0] = "X" is not usually valid.
  4. ASCII arithmetic errors. Remember: , , . The gap between uppercase and lowercase is .
  5. Concatenation order. T = T + S[i] appends to the end. T = S[i] + T prepends to the beginning. The order matters for building reversed or shifted strings.

Contest strategy

  • Write out the string with index numbers above each character for quick reference
  • For filtering/building problems, scan for the pattern rather than tracing every iteration
  • For ASCII problems, write out the key values: , , ,
  • When combining strings with arrays/loops, use a table tracking both the string being built and any numeric variables
  • Typical time: 90-120 seconds per problem