How to resolve the algorithm String comparison step by step in the QB64 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm String comparison step by step in the QB64 programming language

Table of Contents

Problem Statement

Demonstrate how to compare two strings from within the language and how to achieve a lexical comparison.

The task should demonstrate:

For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.

Here "generic/polymorphic" comparison means that the function or operator you're using doesn't always do string comparison, but bends the actual semantics of the comparison depending on the types one or both arguments; with such an operator, you achieve string comparison only if the arguments are sufficiently string-like in type or appearance.
In contrast, a "coercive/allomorphic" comparison function or operator has fixed string-comparison semantics regardless of the argument type;   instead of the operator bending, it's the arguments that are forced to bend instead and behave like strings if they can,   and the operator simply fails if the arguments cannot be viewed somehow as strings.   A language may have one or both of these kinds of operators;   see the Raku entry for an example of a language with both kinds of operators.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm String comparison step by step in the QB64 programming language

Source code in the qb64 programming language

Dim As String String1, String2

' direct string comparison using case sensitive
String1 = "GWbasic"
String2 = "QuickBasic"
If String1 = String2 Then Print String1; " is equal to "; String2 Else Print String1; " is NOT egual to  "; String2
String1 = "gWbasic"
String2 = "GWBasic"
If String1 = String2 Then Print String1; " is equal to "; String2 Else Print String1; " is NOT egual to  "; String2

' direct string comparison using case insensitive
If UCase$(String1) = UCase$(String2) Then Print String1; " is equal to "; String2; Else Print String1; " is NOT egual to  "; String2;
Print " case insensitive"
String1 = "GwBasiC"
String2 = "GWBasic"
If LCase$(String1) = LCase$(String2) Then Print String1; " is equal to "; String2; Else Print String1; " is NOT egual to  "; String2;
Print " case insensitive"

' lexical order
String1 = "AAAbbb"
String2 = "AaAbbb"
If String1 > String2 Then Print String1; " is after "; String2 Else Print String1; " is before "; String2

' number in string format comparison
String1 = "0123"
String2 = "5"
' lexical order
If String1 > String2 Then Print String1; " is after "; String2 Else Print String1; " is before "; String2
' value order
If Val(String1) > Val(String2) Then Print String1; " is bigger than "; String2 Else Print String1; " is lower "; String2

Print "QB64, like QBasic, has native coercive/allomorphic operators for string type variable"
End

  

You may also check:How to resolve the algorithm Simple windowed application step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Numeric error propagation step by step in the Raku programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Ada programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the ZX Spectrum Basic programming language