
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses


Task

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



The task should demonstrate:

 Comparing two strings for exact equality
 Comparing two strings for inequality (i.e., the inverse of exact equality)
 Comparing two strings to see if one is lexically ordered before than the other
 Comparing two strings to see if one is lexically ordered after than the other
 How to achieve both case sensitive comparisons and case insensitive comparisons within the language
 How the language handles comparison of numeric strings if these are not treated lexically
 Demonstrate any other kinds of string comparisons that the language provides, particularly as it relates to your type system. 


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.




Other tasks related to string operations:

Metrics
 Array length
 String length
 Copy a string
 Empty string  (assignment)
Counting
 Word frequency
 Letter frequency
 Jewels and stones
 I before E except after C
 Bioinformatics/base count
 Count occurrences of a substring
 Count how many vowels and consonants occur in a string
Remove/replace
 XXXX redacted
 Conjugate a Latin verb
 Remove vowels from a string
 String interpolation (included)
 Strip block comments
 Strip comments from a string
 Strip a set of characters from a string
 Strip whitespace from a string -- top and tail
 Strip control codes and extended characters from a string
Anagrams/Derangements/shuffling
 Word wheel
 ABC problem
 Sattolo cycle
 Knuth shuffle
 Ordered words
 Superpermutation minimisation
 Textonyms (using a phone text pad)
 Anagrams
 Anagrams/Deranged anagrams
 Permutations/Derangements
Find/Search/Determine
 ABC words
 Odd words
 Word ladder
 Semordnilap
 Word search
 Wordiff  (game)
 String matching
 Tea cup rim text
 Alternade words
 Changeable words
 State name puzzle
 String comparison
 Unique characters
 Unique characters in each string
 Extract file extension
 Levenshtein distance
 Palindrome detection
 Common list elements
 Longest common suffix
 Longest common prefix
 Compare a list of strings 
 Longest common substring
 Find common directory path
 Words from neighbour ones
 Change e letters to i in words
 Non-continuous subsequences
 Longest common subsequence
 Longest palindromic substrings
 Longest increasing subsequence
 Words containing "the" substring
 Sum of the digits of n is substring of n
 Determine if a string is numeric
 Determine if a string is collapsible
 Determine if a string is squeezable
 Determine if a string has all unique characters
 Determine if a string has all the same characters
 Longest substrings without repeating characters
 Find words which contains all the vowels
 Find words which contains most consonants
 Find words which contains more than 3 vowels
 Find words which first and last three letters are equals
 Find words which odd letters are consonants and even letters are vowels or vice_versa
Formatting
 Substring
 Rep-string
 Word wrap
 String case
 Align columns
 Literals/String
 Repeat a string
 Brace expansion
 Brace expansion using ranges
 Reverse a string
 Phrase reversals
 Comma quibbling
 Special characters
 String concatenation
 Substring/Top and tail
 Commatizing numbers
 Reverse words in a string
 Suffixation of decimal numbers
 Long literals, with continuations 
 Numerical and alphabetical suffixes
 Abbreviations, easy
 Abbreviations, simple
 Abbreviations, automatic
Song lyrics/poems/Mad Libs/phrases
 Mad Libs
 Magic 8-ball
 99 Bottles of Beer
 The Name Game (a song)
 The Old lady swallowed a fly
 The Twelve Days of Christmas
Tokenize
 Text between
 Tokenize a string
 Word break problem
 Tokenize a string with escaping
 Split a character string based on change of character
Sequences
 Show ASCII table
 De Bruijn sequences
 Self-referential sequences
 Generate lower case ASCII alphabet




 
/* ARM assembly Raspberry PI  */
/*  program comparString.s   */
 
/* Constantes    */
.equ STDOUT, 1     @ Linux output console
.equ EXIT,   1     @ Linux syscall
.equ WRITE,  4     @ Linux syscall
/* Initialized data */
.data
szMessStringEqu: .asciz "The strings are equals.\n"
szMessStringNotEqu: .asciz "The strings are not equals.\n"
szCarriageReturn:  .asciz "\n"
 
szString1:  .asciz "ABCDE"
szString2:  .asciz "ABCDE"
szString3:  .asciz "ABCFG"
szString4:   .asciz "ABC"
szString5:   .asciz "abcde"
 
/* UnInitialized data */
.bss 
 
/*  code section */
.text
.global main 
main:                /* entry of program  */
    push {fp,lr}    /* saves 2 registers */
 
    ldr r0,iAdrszString1
    ldr r1,iAdrszString2
    bl Comparaison
 
    ldr r0,iAdrszString1
    ldr r1,iAdrszString3
    bl Comparaison
 
    ldr r0,iAdrszString1
    ldr r1,iAdrszString4
    bl Comparaison
 
    @ case sensitive comparisons ABCDE et abcde
    ldr r0,iAdrszString1
    ldr r1,iAdrszString5
    bl Comparaison
 
    @ case insensitive comparisons  ABCDE et abcde
    ldr r0,iAdrszString1
    ldr r1,iAdrszString5
    bl comparStringsInsensitive
    cmp r0,#0
    bne 1f
    ldr r0,iAdrszMessStringEqu
    bl affichageMess
    b 2f
1:
    ldr r0,iAdrszMessStringNotEqu
    bl affichageMess
 
2:
 
100:   /* standard end of the program */
    mov r0, #0                  @ return code
    pop {fp,lr}                 @restaur 2 registers
    mov r7, #EXIT              @ request to exit program
    swi 0                       @ perform the system call
iAdrszString1: .int szString1
iAdrszString2: .int szString2
iAdrszString3: .int szString3
iAdrszString4: .int szString4
iAdrszString5: .int szString5
iAdrszMessStringEqu:  .int szMessStringEqu
iAdrszMessStringNotEqu:  .int szMessStringNotEqu
iAdrszCarriageReturn:  .int  szCarriageReturn
/*********************************************/
/* comparaison                               */
/*********************************************/
/* r0 contains address String 1           */
/* r1 contains address String 2         */
Comparaison: 
    push {fp,lr}    			/* save  registres */ 
    bl comparStrings
    cmp r0,#0
    bne 1f
    ldr r0,iAdrszMessStringEqu
    bl affichageMess
    b 2f
1:
    ldr r0,iAdrszMessStringNotEqu
    bl affichageMess
 
2:
    pop {fp,lr}    				/* restaur des  2 registres */ 
    bx lr	        			/* return  */
/******************************************************************/
/*     display text with size calculation                         */ 
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
    push {fp,lr}    			/* save  registres */ 
    push {r0,r1,r2,r7}    		/* save others registers */
    mov r2,#0   				/* counter length */
1:      	/* loop length calculation */
    ldrb r1,[r0,r2]  			/* read octet start position + index */
    cmp r1,#0       			/* if 0 its over */
    addne r2,r2,#1   			/* else add 1 in the length */
    bne 1b          			/* and loop */
                                /* so here r2 contains the length of the message */
    mov r1,r0        			/* address message in r1 */
    mov r0,#STDOUT      		/* code to write to the standard output Linux */
    mov r7, #WRITE             /* code call system "write" */
    swi #0                      /* call systeme */
    pop {r0,r1,r2,r7}     		/* restaur others registers */
    pop {fp,lr}    				/* restaur des  2 registres */ 
    bx lr	        			/* return  */
/************************************/	   
/* Strings case sensitive comparisons  */
/************************************/	  
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1  if string r0 > string r1 */
comparStrings:
    push {r1-r4}  /* save des registres */
    mov r2,#0   /* counter */
1:	
    ldrb r3,[r0,r2]   /* byte string 1 */
    ldrb r4,[r1,r2]   /* byte string 2 */
    cmp r3,r4
    movlt r0,#-1	 /* small */ 	
    movgt r0,#1	 /* greather */ 	
    bne 100f     /* not equals */
    cmp r3,#0   /* 0 end string */
    moveq r0,#0    /* equals */ 	
    beq 100f     /*  end string */
    add r2,r2,#1 /* else add 1 in counter */
    b 1b         /* and loop */
100:
    pop {r1-r4}
    bx lr   
 
/************************************/	   
/* Strings case insensitive comparisons    */
/************************************/	  
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1  if string r0 > string r1 */
comparStringsInsensitive:
    push {r1-r4}  /* save des registres */
    mov r2,#0   /* counter */
 
1:	
    ldrb r3,[r0,r2]   /* byte string 1 */
    ldrb r4,[r1,r2]   /* byte string 2 */
    @ majuscules --> minuscules  byte 1
    cmp r3,#65
    blt 2f
    cmp r3,#90
    bgt 2f
    add r3,#32
2:   	@ majuscules --> minuscules  byte 2
    cmp r4,#65
    blt 3f
    cmp r4,#90
    bgt 3f
    add r4,#32
3:	
    cmp r3,r4
    movlt r0,#-1	 /* small */ 	
    movgt r0,#1	 /* greather */ 	
    bne 100f     /* not equals */
    cmp r3,#0   /* 0 end string */
    moveq r0,#0    /* equal */ 	
    beq 100f     /* end strings */
    add r2,r2,#1 /* else add 1 in counter */
    b 1b         /* and loop */
100:
    pop {r1-r4}
    bx lr   	/* end procedure */
 
 