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

→‎{{header|S-BASIC}}: revamp output display
(Added Chipmunk Basic)
(→‎{{header|S-BASIC}}: revamp output display)
 
(4 intermediate revisions by 4 users not shown)
Line 912:
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <stringstring_view>
 
void all_characters_are_the_same(const std::string&string_view str) {
size_t len = str.length();
std::cout << "input: \"" << str << "\", length: " << len << '\n';
Line 922:
if (str[i] != ch) {
std::cout << "Not all characters are the same.\n";
std::cout << "Character '" << str[i] << "' (hex " << std::hex
<< "' (hex " << std::hex << static_cast<unsigned int>(str[i])
<< ") at position " << std::dec << i + 1
<< " is not the same as '" << ch << "'.\n\n";
return;
}
Line 1,329:
"tttTTT" — length 6 — contains a different character at index 3: 'T' (0x54)
"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>
=={{header|Fortran}}==
Line 3,461 ⟶ 3,492:
Checking string "🎄🎄🎄🎄" of length 4:
All characters in the string are the same
</pre>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang = "BASIC">
comment
Return 0 if all the characters in s are the same
(including the special case of an empty string),
otherwise the first position where a character
differs from the preceeding one(s)
end
function samechars(s = string) = integer
var i, slen, result = integer
slen = len(s)
i = 1
while i < slen and mid(s,i,1) = mid(s,i+1,1) do
i = i+1
if i = slen or slen = 0 then
result = 0
else
result = i+1
end = result
 
procedure report(str = string)
var p = integer
print "Test string "; chr(34); str; chr(34); \
" has length ="; len(str)
p = samechars(str)
if p = 0 then
print "The characters are all the same"
else
print "Characters differ starting at position";p;" ('"; \
mid(str,p,1);"'=";right$(hex$(asc(mid(str,p,1))),2);"h)"
print
end
 
rem - apply to the test cases
 
report ""
report " "
report "2"
report "333"
report ".55"
report "tttTTT"
report "4444 444k"
 
end
</syntaxhighlight>
{{out}}
<pre>
Test string "" has length = 0
The characters are all the same
 
Test string " " has length = 0
The characters are all the same
 
Test string "2" has length = 1
The characters are all the same
 
Test string "333" has length = 3
The characters are all the same
 
Test string ".55" has length = 3
Characters differ starting at position 2 ('5'=35h)
 
Test string "tttTTT" has length = 6
Characters differ starting at position 4 ('T'=54h)
 
Test string "4444 444k" has length = 9
Characters differ starting at position 5 (' '=20h)
</pre>
 
Line 3,872 ⟶ 3,972:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var analyze = Fn.new { |s|
18

edits