Determine if a string has all the same characters: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
imported>Fth No edit summary |
||
Line 1,329: | Line 1,329: | ||
"tttTTT" — length 6 — contains a different character at index 3: 'T' (0x54) |
"tttTTT" — length 6 — contains a different character at index 3: 'T' (0x54) |
||
"4444 444k" — length 9 — contains a different character at index 4: ' ' (0x20) |
"4444 444k" — length 9 — contains a different character at index 4: ' ' (0x20) |
||
</pre> |
|||
=={{header|Forth}}== |
|||
<syntaxhighlight lang="forth"> |
|||
: samechars? ( str-addr str-len -- ) |
|||
[char] " emit 2dup type [char] " emit ." length: " dup . ." -> " |
|||
dup 1 > if |
|||
over c@ swap 1 do |
|||
over i + c@ over <> if |
|||
." different character '" drop i + c@ dup emit |
|||
." ' ($" hex 1 .r ." ) at " decimal i . cr unloop exit |
|||
then |
|||
loop |
|||
then 2drop ." all characters are the same" cr ; |
|||
s" " samechars? |
|||
s" " samechars? |
|||
s" 2" samechars? |
|||
s" 333" samechars? |
|||
s" .55" samechars? |
|||
s" tttTTT" samechars? |
|||
s" 4444 444k" samechars? |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
"" length: 0 -> all characters are the same |
|||
" " length: 3 -> all characters are the same |
|||
"2" length: 1 -> all characters are the same |
|||
"333" length: 3 -> all characters are the same |
|||
".55" length: 3 -> different character '5' ($35) at 1 |
|||
"tttTTT" length: 6 -> different character 'T' ($54) at 3 |
|||
"4444 444k" length: 9 -> different character ' ' ($20) at 4 |
|||
</pre> |
</pre> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |