Jump to content

Selectively replace multiple instances of a character within a string: Difference between revisions

Selectively replace multiple instances of a character within a string in FreeBASIC
(Selectively replace multiple instances of a character within a string in FreeBASIC)
Line 82:
caaarrbabad -> cABarFECbDd
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>Function replaceChar(Byref S As String) As String
Dim As String A = "ABaCD", B = "Eb", R = "rF"
Dim As Byte pA = 1, pB = 1, pR = 1
For i As Byte = 0 To Len(S)
Select Case Mid(S,i,1)
Case "a"
Mid(S,i,1) = Mid(A,pA,1)
pA += 1
Case "b"
Mid(S,i,1) = Mid(B,pB,1)
pB += 1
Case "r"
Mid(S,i,1) = Mid(R,pR,1)
pR += 1
End Select
Next i
Return S
End Function
 
Dim As String S
S = "abracadabra"
Print S; " -> "; replaceChar(S)
S = "caarabadrab"
Print S; " -> "; replaceChar(S)
Sleep</lang>
{{out}}
<pre>abracadabra -> AErBcadCbFD
caaarrbabad -> cABarFECbDd</pre>
 
=={{header|Go}}==
2,136

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.