RosettaCodeData/Task/Abbreviations-easy/FreeBASIC/abbreviations-easy.basic
2024-04-19 16:56:29 -07:00

70 lines
2.4 KiB
Text

Dim As String table = "Add ALTer BAckup Bottom CAppend Change SCHANGE CInsert CLAst COMPress COpy " _
+ "COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find " _
+ "NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput " _
+ "Join SPlit SPLTJOIN LOAD Locate CLocate LOWercase UPPercase LPrefix MACRO " _
+ "MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD Query QUIT " _
+ "READ RECover REFRESH RENum REPeat Replace CReplace RESet RESTore RGTLEFT " _
+ "RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up"
Function NextWord(Byref posic As Integer, text As String) As String
' skip spaces
While posic <= Len(text) And Mid(text, posic, 1) = " "
posic += 1
Wend
' get the word
Dim word As String = ""
While posic <= Len(text) And Mid(text, posic, 1) <> " "
word += Mid(text, posic, 1)
posic += 1
Wend
Return word
End Function
Function MinABLength(comando As String) As Integer
Dim ab_min As Integer = 1
While ab_min <= Len(comando) And Ucase(Mid(comando, ab_min, 1)) = Mid(comando, ab_min, 1)
ab_min += 1
Wend
Return ab_min - 1
End Function
Function Expand(table As String, word As String) As String
If Len(word) = 0 Then
Return ""
Else
Dim As Integer word_len = Len(word)
Dim As String result = "*error*"
Dim As Integer posic = 1
Do
Dim As String comando = NextWord(posic, table)
If Len(comando) = 0 Then
Exit Do
Elseif word_len < MinABLength(comando) Or word_len > Len(comando) Then
Continue Do
Elseif Ucase(word) = Ucase(Left(comando, word_len)) Then
result = Ucase(comando)
Exit Do
End If
Loop
Return result
End If
End Function
Sub TestExpand(words As String, table As String)
Dim As String word, results = "", separator = ""
Dim As Integer posic = 1
Do
word = NextWord(posic, words)
If Len(word) = 0 Then Exit Do
results += separator + Expand(table, word)
separator = " "
Loop
Print "Input: "; words
Print "Output: "; results
End Sub
' task test cases
TestExpand("riG rePEAT copies put mo rest types fup. 6 poweRin", table)
TestExpand("", table)
Sleep