Compare length of two strings

Revision as of 00:54, 28 October 2021 by Puppydrum64 (talk | contribs) (Created page with "{{task|String manipulation}} {{basic data operation}} Category: String manipulation Category:Simple ;Task: Given two strings of different length, determine which s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer one first.

Task
Compare length of two strings
You are encouraged to solve this task according to the task description, using any language you may know.

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


Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences



Z80 Assembly

<lang z80>Terminator equ 0 ;null terminator PrintChar equ &BB5A ;Amstrad CPC BIOS call, prints accumulator to screen as an ASCII character.

       org &8000

ld hl,String1 ld de,String2 call CompareStringLengths

jp nc, Print_HL_First ex de,hl Print_HL_First: push bc push hl call PrintString pop hl push hl ld a,' ' call PrintChar call getStringLength ld a,b call ShowHex_NoLeadingZeroes call NewLine pop hl pop bc

ex de,hl push bc push hl call PrintString pop hl push hl ld a,' ' call PrintChar call getStringLength ld a,b call ShowHex_NoLeadingZeroes call NewLine pop hl pop bc ReturnToBasic: RET

String1: byte "Hello",Terminator String2: byte "Goodbye",Terminator

RELEVANT SUBROUTINES - SOME OF THESE CREATED BY KEITH S. OF CHIBIAKUMAS

NewLine: push af ld a,13 ;Carriage return call PrintChar ld a,10 ;Line Feed call PrintChar pop af ret

PrintString: ld a,(hl) cp Terminator ret z inc hl call PrintChar jr PrintString

GetStringLength: ld b,0 loop_getStringLength: ld a,(hl) cp Terminator ret z inc hl inc b jr loop_getStringLength ShowHex_NoLeadingZeroes:

useful for printing values where leading zeroes don't make sense,
such as money etc.

push af and %11110000 ifdef gbz80 ;game boy swap a else ;zilog z80 rrca rrca rrca rrca endif cp 0 call nz,PrintHexChar ;if top nibble of A is zero, don't print it. pop af and %00001111 cp 0 ret z ;if bottom nibble of A is zero, don't print it! call PrintHexChar

PrintHexChar: or a ;Clear Carry Flag daa add a,&F0 adc a,&40 ;This sequence converts a 4-bit hex digit to its ASCII equivalent. jp PrintChar</lang>