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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm String comparison step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0

' Strings in FB natively support the relational operators which compare lexically on a case-sensitive basis.
' There are no special provisions for numerical strings.
' There are no other types of string comparison for the built-in types though 'user defined types'
' can specify their own comparisons by over-loading the relational operators.

Function StringCompare(s1 As Const String, s2 As Const String, ignoreCase As Boolean = false) As String
  Dim As String s, t ' need new string variables as the strings passed in can't be changed
  If ignoreCase Then
    s = LCase(s1)
    t = LCase(s2)
  Else
    s = s1
    t = s2
  End If
  If s < t Then Return " comes before "
  If s = t Then Return " is equal to "
  Return " comes after "
End Function

Dim As Integer result
Dim As String s1, s2, s3
s1 = "Dog" : s2 = "Dog"
Print s1; StringCompare(s1, s2); s2
s2 = "Cat"
Print s1; StringCompare(s1, s2); s2
s2 = "Rat"
Print s1; StringCompare(s1, s2); s2
s2 = "dog"
Print s1; StringCompare(s1, s2); s2
Print s1; StringCompare(s1, s2, True); s2; " if case is ignored"
s1  = "Dog" : s2 = "Pig"
s3 = StringCompare(s1, s2)
If s3 <> " is equal to " Then
  Print s1; " is not equal to "; s2
End If
Print
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm CSV data manipulation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Host introspection step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Loops/For step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Magic squares of singly even order step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the FreeBASIC programming language