There are two ways to answer this.
The first is a simple run-length encoding scheme, where the input cabc
results in c1 a1 b1 c1
. This has the benefit that you can often output things immediately with a fairly low memory requirement to boot:
input-string := Get-Input
(* nil is the representation of no characters here. *)
last-char := nil
count := 0
For Each (char c) In input-string
If c = last-char Then
count := count + 1
Else
If last-char = nil Then
count := 1
last-char := c
Else
Display last-char, count
count := 1
last-char := c
End If
End If
Loop
If count != 0 Then
Display last-char, count
End If
The other solution I conceived will preserve the order and determine the counts of all unique letters in the string, yielding c2 a1 b1
for an input of cabc
. This solution is a bit more complex and requires more memory and often more execution time, but it results in more compact output due to the lack of repeated letters:
input-string := Get-Input
(* 26 is the number of letters a-z. *)
counts := Create-Array 26
order-string := ""
For Each (char c) In input-string
i := Locate-Char order-string, c
If i = 0 Then
order-string := order-string + c
counts [Length order-string] := 1
Else
counts [i] := counts [i] + 1
End If
Loop
For i := 1 To (Length order-string)
Display (Char-At order-string, i), counts [i]
Loop
The first should be straightforward to convert to QBASIC, but you might need to use the help file to learn about the DIM
keyword and how to use it to create an array. This algorithm assumes that arrays start at 1, not 0.