Module 1
Strings and StringBuilders
Character Arrays
CC315
Array of Strings
arr = ["a","b","abc"]
Array of Strings
arr = []
arr.append("foo")
arr.append("1234") # here arr == ["foo","1234"]
length = len(arr) # will result in 2
string_arr = "".join(arr) # will result in "foo1234"
With Strings
function ENCODER(TEXT,X)
#TEXT is the text to encode
#X is the offset
ENC = ""
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC += CHAR_ENC
ELSE
ENC += '*'
end loop
return ENC
end function
With Array of Strings
function ARR_ENCODER(TEXT,X)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
With Strings
function ENCODER(TEXT,X)
#TEXT is the text to encode
#X is the offset
ENC = ""
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC += CHAR_ENC
ELSE
ENC += '*'
end loop
return ENC
end function
With Array of Strings
function ARR_ENCODER(TEXT,X)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Array of Strings Walk Through
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Array of Strings Walk Through
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Array of Strings Walk Through
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Array of Strings Walk Through
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Array of Strings Walk Through
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Fast Forward
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Fast Forward
function ARR_ENCODER(TEXT,X) # call ARR_ENCODER("Data is great!",8)
#TEXT is the text to encode
#X is the offset
ENC = []
loop I from 1 to LENGTH of TEXT
CURRENT = TEXT[I]
IF CURRENT IS A LETTER
CHAR_ENC = GET X-th CHAR after CURRENT
ENC.append(CHAR_ENC)
ELSE
ENC.append('*')
end loop
return "".join(ENC)
end function
Comparison:
String (S) VS Array of Strings (ARR)
Iterations |
Character Copies |
Memory Addresses |
|
S |
ARR |
S |
ARR |
n |
(n(n+1))/2 |
n |
n + 1 |
1 |
1,000,000 |
500,000,500,000 |
1,000,000 |
1,000,001 |
1 |