RosettaCodeData/Task/Determine-if-a-string-has-all-the-same-characters/S-BASIC/determine-if-a-string-has-all-the-same-characters.basic
2024-04-19 16:56:29 -07:00

42 lines
952 B
Text

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