Determine if a string has all the same characters: Difference between revisions
Content deleted Content added
Add ed example |
Added OmniMark solution |
||
Line 2,509: | Line 2,509: | ||
Input = "4444 444k", length = 9 |
Input = "4444 444k", length = 9 |
||
First difference at position 5, character = ' ', hex = 0x20 |
First difference at position 5, character = ' ', hex = 0x20 |
||
</pre> |
|||
=={{header|OmniMark}}== |
|||
This takes it a bit further, including characters other than ASCII. |
|||
<syntaxhighlight lang="omnimark"> |
|||
; Command line: omnimark -sb determine.xom -of determine.txt |
|||
include "utf8pat.xin" |
|||
process |
|||
local stream s variable initial {'', ' ', '2', '333', '.55', 'tttTTT', '4444 444k', '😁😁🙂😁😁'} |
|||
repeat over s |
|||
local stream last initial {''} |
|||
local switch same initial {true} |
|||
local stream poschar |
|||
local integer poshex |
|||
local integer poshex2 |
|||
local integer position initial {1} |
|||
local integer len initial {0} |
|||
repeat scan s |
|||
match (["%0#" to "%127#"] | utf8-char) => char |
|||
do when last = '' |
|||
set last to char |
|||
else when last != char |
|||
set poschar to char when same |
|||
deactivate same |
|||
do when char matches utf8-char |
|||
set poshex to utf8-char-number char |
|||
else |
|||
set poshex to char binary 0 |
|||
done |
|||
done |
|||
increment position when same |
|||
increment len |
|||
again |
|||
output 'String "%g(s)" is %d(len) character' |
|||
output 's' unless len = 1 |
|||
output ':%n' |
|||
do when same |
|||
output ' - All characters are the same.%n' |
|||
else |
|||
output ' - Not all characters are the same (position %d(position): "%g(poschar)" [hex=%16rud(poshex)])%n' |
|||
done |
|||
again |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
String "" is 0 characters: |
|||
- All characters are the same. |
|||
String " " is 3 characters: |
|||
- All characters are the same. |
|||
String "2" is 1 character: |
|||
- All characters are the same. |
|||
String "333" is 3 characters: |
|||
- All characters are the same. |
|||
String ".55" is 3 characters: |
|||
- Not all characters are the same (position 2: "5" [hex=35]) |
|||
String "tttTTT" is 6 characters: |
|||
- Not all characters are the same (position 4: "T" [hex=54]) |
|||
String "4444 444k" is 9 characters: |
|||
- Not all characters are the same (position 5: " " [hex=6B]) |
|||
String "😁😁🙂😁😁" is 5 characters: |
|||
- Not all characters are the same (position 3: "🙂" [hex=1F642]) |
|||
</pre> |
</pre> |
||