Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Repeat-a-string/FreeBASIC/repeat-a-string.freebasic
Normal file
29
Task/Repeat-a-string/FreeBASIC/repeat-a-string.freebasic
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' A character is essentially a string of length 1 in FB though there is a built-in function, String,
|
||||
' which creates a string by repeating a character a given number of times.
|
||||
|
||||
' To avoid repeated concatenation (a slow operation) when the string to be repeated has a length
|
||||
' greater than one, we instead create a buffer of the required size and then fill that.
|
||||
|
||||
Function repeat(s As String, n As Integer) As String
|
||||
If n < 1 Then Return ""
|
||||
If n = 1 Then Return s
|
||||
Var size = Len(s)
|
||||
If size = 0 Then Return s ' empty string
|
||||
If size = 1 Then Return String(n, s[0]) ' repeated single character
|
||||
Var buffer = Space(size * n) 'create buffer for size > 1
|
||||
For i As Integer = 0 To n - 1
|
||||
For j As Integer = 0 To size - 1
|
||||
buffer[i * size + j] = s[j]
|
||||
Next j
|
||||
Next i
|
||||
Return buffer
|
||||
End Function
|
||||
|
||||
Print repeat("rosetta", 1)
|
||||
Print repeat("ha", 5)
|
||||
Print repeat("*", 5)
|
||||
Print
|
||||
Print "Press any key to quit program"
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue