27 lines
519 B
Plaintext
27 lines
519 B
Plaintext
// CALL: COUNT_BASE('A', "GATTACA")
|
|
// EXPECT: COUNT_BASE returned 3
|
|
// How many positions of T hold the base B.
|
|
FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER
|
|
|
|
// Set up
|
|
DECLARE N : INTEGER
|
|
DECLARE Total : INTEGER
|
|
N <- LENGTH(T)
|
|
Total <- 0
|
|
|
|
// Visit each position
|
|
FOR I <- 0 TO N
|
|
|
|
// Test this position
|
|
IF T[I] == B THEN
|
|
|
|
// Update the running total
|
|
Total <- Total + 1
|
|
|
|
ENDIF
|
|
NEXT I
|
|
|
|
// Report
|
|
RETURN Total
|
|
ENDFUNCTION
|