How to resolve the algorithm String comparison step by step in the Action! programming language
How to resolve the algorithm String comparison step by step in the Action! 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 Action! programming language
Source code in the action! programming language
PROC TestEqual(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" and ""%S"" are equal: ",s1,s2)
IF SCompare(s1,s2)=0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestInequal(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" and ""%S"" are inequal: ",s1,s2)
IF SCompare(s1,s2)#0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestBefore(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" is before ""%S"": ",s1,s2)
IF SCompare(s1,s2)<0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestAfter(CHAR ARRAY s1,s2)
INT res
PrintF("""%S"" is after ""%S"": ",s1,s2)
IF SCompare(s1,s2)>0 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumEqual(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" and ""%S"" are equal: ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1=v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumInequal(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" and ""%S"" are inequal: ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1#v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumBefore(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" is before ""%S"": ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC TestNumAfter(CHAR ARRAY s1,s2)
INT v1,v2
PrintF("""%S"" is after ""%S"": ",s1,s2)
v1=ValI(s1) v2=ValI(s2)
IF v1>v2 THEN
PrintE("True")
ELSE
PrintE("False")
FI
RETURN
PROC Main()
PrintE("Lexical comparison:")
TestEqual("abcd","Abcd")
TestInequal("abcd","Abcd")
TestBefore("abcd","Abcd")
TestAfter("abcd","Abcd")
PutE()
PrintE("Numerical comparison:")
TestNumEqual("1234","99876")
TestNumInequal("1234","99876")
TestNumBefore("1234","99876")
TestNumAfter("1234","99876")
RETURN
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Quackery programming language
You may also check:How to resolve the algorithm Superpermutation minimisation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the jq programming language
You may also check:How to resolve the algorithm Almost prime step by step in the RPL programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the R programming language