How to resolve the algorithm Run-length encoding step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Run-length encoding step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression. The output can be anything, as long as you can recreate the input with it.
Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Run-length encoding step by step in the FreeBASIC programming language
Source code in the freebasic programming language
Dim As String initial, encoded, decoded
Function RLDecode(i As String) As String
Dim As Long Loop0
dim as string rCount, outP, m
For Loop0 = 1 To Len(i)
m = Mid(i, Loop0, 1)
Select Case m
Case "0" To "9"
rCount += m
Case Else
If Len(rCount) Then
outP += String(Val(rCount), m)
rCount = ""
Else
outP += m
End If
End Select
Next
RLDecode = outP
End Function
Function RLEncode(i As String) As String
Dim As String tmp1, tmp2, outP
Dim As Long Loop0, rCount
tmp1 = Mid(i, 1, 1)
tmp2 = tmp1
rCount = 1
For Loop0 = 2 To Len(i)
tmp1 = Mid(i, Loop0, 1)
If tmp1 <> tmp2 Then
outP += Ltrim(Rtrim(Str(rCount))) + tmp2
tmp2 = tmp1
rCount = 1
Else
rCount += 1
End If
Next
outP += Ltrim(Rtrim(Str(rCount)))
outP += tmp2
RLEncode = outP
End Function
Input "Type something: ", initial
encoded = RLEncode(initial)
decoded = RLDecode(encoded)
Print initial
Print encoded
Print decoded
End
You may also check:How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the FreeBASIC programming language