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

→‎{{header|S-BASIC}}: revamp output display
m (C++ - replaced string by string_view)
(→‎{{header|S-BASIC}}: revamp output display)
 
(One intermediate revision by the same user not shown)
Line 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>
 
18

edits