Determine if a string has all the same characters: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(Add Erlang version) |
||
Line 986: | Line 986: | ||
" " 0x20 is different at position 5 |
" " 0x20 is different at position 5 |
||
</pre> |
</pre> |
||
=={{header|Erlang|Erlang}}== |
|||
<lang erlang> |
|||
-module(string_examples). |
|||
-export([examine_all_same/1, all_same_examples/0]). |
|||
all_same_characters([], _Offset) -> |
|||
all_same; |
|||
all_same_characters([_], _Offset) -> |
|||
all_same; |
|||
all_same_characters([X, X | Rest], Offset) -> |
|||
all_same_characters([X | Rest], Offset + 1); |
|||
all_same_characters([X, Y | _Rest], Offset) when X =/= Y -> |
|||
{not_all_same, Y, Offset + 1}. |
|||
examine_all_same(String) -> |
|||
io:format("String \"~ts\" of length ~p:~n", [String, length(String)]), |
|||
case all_same_characters(String, 0) of |
|||
all_same -> |
|||
io:format(" All characters are the same.~n~n"); |
|||
{not_all_same, OffendingChar, Offset} -> |
|||
io:format(" Not all characters are the same.~n"), |
|||
io:format(" Char '~tc' (0x~.16b) at offset ~p differs.~n~n", |
|||
[OffendingChar, OffendingChar, Offset]) |
|||
end. |
|||
all_same_examples() -> |
|||
Strings = ["", |
|||
" ", |
|||
"2", |
|||
"333", |
|||
".55", |
|||
"tttTTT", |
|||
"4444 444k", |
|||
"pΓ©pΓ©", |
|||
"πΆπΆπΊπΆ", |
|||
"ππππ"], |
|||
lists:foreach(fun examine_all_same/1, Strings). |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
$ erl |
|||
Erlang/OTP 23 [erts-11.1.8] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] |
|||
Eshell V11.1.8 (abort with ^G) |
|||
1> c(string_examples). |
|||
{ok,string_examples} |
|||
2> string_examples:all_same_examples(). |
|||
String "" of length 0: |
|||
All characters are the same. |
|||
String " " of length 3: |
|||
All characters are the same. |
|||
String "2" of length 1: |
|||
All characters are the same. |
|||
String "333" of length 3: |
|||
All characters are the same. |
|||
String ".55" of length 3: |
|||
Not all characters are the same. |
|||
Char '5' (0x35) at offset 1 differs. |
|||
String "tttTTT" of length 6: |
|||
Not all characters are the same. |
|||
Char 'T' (0x54) at offset 3 differs. |
|||
String "4444 444k" of length 9: |
|||
Not all characters are the same. |
|||
Char ' ' (0x20) at offset 4 differs. |
|||
String "pΓ©pΓ©" of length 4: |
|||
Not all characters are the same. |
|||
Char 'Γ©' (0xe9) at offset 1 differs. |
|||
String "πΆπΆπΊπΆ" of length 4: |
|||
Not all characters are the same. |
|||
Char 'πΊ' (0x1f43a) at offset 2 differs. |
|||
String "ππππ" of length 4: |
|||
All characters are the same. |
|||
ok |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<lang fsharp> |
<lang fsharp> |
||
Line 1,014: | Line 1,099: | ||
First different character in <<<4444 444k>>> (length 9) is hex 20 at position 4 |
First different character in <<<4444 444k>>> (length 9) is hex 20 at position 4 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor>USING: formatting io kernel math.parser sequences ; |
<lang factor>USING: formatting io kernel math.parser sequences ; |