Determine if a string has all the same characters: Difference between revisions

Content added Content deleted
(Add Erlang version)
(Added solution for Action!)
Line 75: Line 75:
Not all characters in the string are the same.
Not all characters in the string are the same.
' ' (0x20) is different at position 4
' ' (0x20) is different at position 4
</pre>

=={{header|Action!}}==
<lang Action!>PROC PrintBH(BYTE a)
BYTE ARRAY hex=['0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F]

Put(hex(a RSH 4))
Put(hex(a&$0F))
RETURN

PROC Test(CHAR ARRAY s)
BYTE i,pos

pos=0
FOR i=2 TO s(0)
DO
IF s(i)#s(1) THEN
pos=i
EXIT
FI
OD

PrintF("""%S"" (len=%B) -> ",s,s(0))
IF pos=0 THEN
PrintE("all characters are the same.")
ELSE
PrintF("""%C"" (hex=$",s(pos))
PrintBH(s(pos))
PrintF(") is the first difference at pos. %B.%E",pos)
FI
PutE()
RETURN

PROC Main()
Test("")
Test(" ")
Test("2")
Test("333")
Test(".55")
Test("tttTTT")
Test("4444 444k")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Determine_if_a_string_has_all_the_same_characters.png Screenshot from Atari 8-bit computer]
<pre>
"" (len=0) -> all characters are the same.

" " (len=3) -> all characters are the same.

"2" (len=1) -> all characters are the same.

"333" (len=3) -> all characters are the same.

".55" (len=3) -> "5" (hex=$35) is the first difference at pos. 2.

"tttTTT" (len=6) -> "T" (hex=$54) is the first difference at pos. 4.

"4444 444k" (len=9) -> " " (hex=$20) is the first difference at pos. 5.
</pre>
</pre>