PDA

View Full Version : reverse the words



zak
25-01-2011, 12:39
i want to send a letter to a friend but in reverse, not completely reversed ; but only the words: like: dear george how are you ---> you are how george dear, i come to the following way:
type the normal message in the text box then click Run button

Uses "UI"
Dim MyMatrix() As String
Dim nLines,i As Long
Dim tmpStr, txt As String
Global hDlg As DWord
Function TBMain()
Dialog New 0, "reverse the words", -1, -1, 300, 220, _
%WS_DLGFRAME Or %DS_CENTER Or %WS_CAPTION Or %WS_SYSMENU Or %WS_OVERLAPPEDWINDOW, _
0 To hDlg

Control Add Textbox , hDlg, 3, "", 5, 20, 290, 200, _
%ES_MULTILINE Or _
%ES_AUTOHSCROLL Or _
%ES_AUTOVSCROLL Or _
%WS_VSCROLL Or _
%WS_HSCROLL
Control Add Button , hdlg, 2,"Run",5,0,40,20
Dialog Show Modal hDlg, Call dlgProc
End Function
CallBack Function dlgProc() As Long
Select Case CBMSG
Case %WM_COMMAND
Select Case CBCTL
Case 2
tmpStr="" : txt=""
Control Get Text CBHNDL, 3 To txt
txt = TrimFull$(txt)
nLines = Parse(txt, MyMatrix, " ")
For i =nLines To 1 Step -1
tmpStr = tmpStr & MyMatrix(i) & " "
Next
'txt = txt & CRLF & "-----------" & & CRLF & tmpStr
txt = txt & CRLF & "-----------" & CRLF & tmpStr
Control Set Text hDlg, 3, txt

End Select
End Select
End Function

i have found TRIMFULL$ very usefull to reduce the many spaces to one, i was about to use regex but then have found this handy function. we must not use regular expressions but as a last resort for the complex pattern.
i found also that writing "by mistake" & & such as:
txt = txt & CRLF & "-----------" & & CRLF & tmpStr
hangs the program instead of giving an error message.
if there are other methods of reversing the words please post.
regards

Petr Schreiber
25-01-2011, 19:14
Hi Zak,

I posted the parser problem here:
Bug report (http://www.thinbasic.com/community/project.php?do=gotonote&issuenoteid=1690)

Thanks for your code, I also like to cypher messages sometimes :)


Petr

ErosOlmi
25-01-2011, 22:55
Thanks Petr.

This week I restarted to code on thinAir and thinBasic after my long heavy work load at office so I fix asap.

Ciao
Eros