I'm learning Smalltalk programming as I think its way ahead as an OOP programming tool. I would appreciate feedback regarding my coding of the following problem. I have a sequence of letters representing bases in a DNA and want to change the T to a U in the string. I have done this in two ways. The first uses the
copyReplaceAll:with: message to the string object which changes every occurrence of 'T' to a 'U':
string := 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'. string copyReplaceAll: 'T' with: 'U'.
My second method uses iteration and boolean conditions to test whether a 'T' is identified. I learned that I have to copy the string to an Array object, then iterate over that object and copy the elements to an empty array. Ideally I would like the original string object to be mutable. Am I going at this the wrong way? Here's my code:
string := 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'. strCopy := string asArray. empty := Array new: strCopy size. cntr := 1. strCopy do: [:char | (char = $T) ifTrue: [empty at:cntr put: $U ] ifFalse: [empty at:cntr put: char]. cntr := cntr + 1 ].