String comparison: Difference between revisions

Added uBasic/4tH version
(String comparison in various BASIC dialents (BASIC256, QBasic, True BASIC and Yabasic))
(Added uBasic/4tH version)
Line 1,217:
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
==={{header|uBasic/4tH}}===
{{works with|R3}}
uBasic/4tH provides a builtin, case insensitive function to compare two strings, called <code>COMP()</code> which returns either a negative, zero or positive value, just like <code>strcmp()</code>. In order to compare two strings case sensitive, a user defined function is required.
<lang>Print "Case sensitive"
Print "=============="
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Dog"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Cat"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Rat"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("dog"))))))
Print Show (FUNC(_Eval(FUNC(_StrCmp (Dup ("Dog"), Dup ("Pig"))))))
 
Print
 
Print "Case insensitive"
Print "================"
Print Show (FUNC(_Eval(Comp ("Dog", "Dog"))))
Print Show (FUNC(_Eval(Comp ("Dog", "Cat"))))
Print Show (FUNC(_Eval(Comp ("Dog", "Rat"))))
Print Show (FUNC(_Eval(Comp ("Dog", "dog"))))
Print Show (FUNC(_Eval(Comp ("Dog", "Pig"))))
 
End
 
_StrCmp ' case sensitive compare
Param (2)
Local (3)
' up to the maximum length
For c@ = 0 To Max (Len (a@), Len (b@)) - 1
d@ = Iif (c@ < Len (a@), Peek (a@, c@), 0)
e@ = Iif (c@ < Len (b@), Peek (b@, c@), 0)
While (d@ = e@) ' while retrieved characters are equal
Next
 
Return (d@ - e@) ' return comparison
 
_Eval ' evaluate result
Param (1)
If a@ = 0 Then Return (Dup ("Equal"))
If a@ > 0 Then Return (Dup ("Second before First"))
Return (Dup ("First before Second"))</lang>
Output:
<pre>
Case sensitive
==============
Equal
Second before First
First before Second
First before Second
First before Second
 
Case insensitive
================
Equal
Second before First
First before Second
Equal
First before Second
 
0 OK, 0:673
</pre>
 
=={{header|BBC BASIC}}==
374

edits