Jump to content

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

Added S-BASIC example
m (C++ - replaced string by string_view)
(Added S-BASIC example)
Line 3,493:
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
 
rem - examine test cases
 
var str = string
var p = integer
 
while 1 = 1 do
begin
rem - ^C at prompt will end program
input "string to test: "; str
print "The 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 at position";p;" ('"; \
mid(str,p,1);"'=";right$(hex$(asc(mid(str,p,1))),2);"h)"
print
end
 
end
</syntaxhighlight>
{{out}}
<pre>
string to test:
The test string "" has length = 0
The characters are all the same
 
string to test:
The test string " " has length = 0
The characters are all the same
 
string to test: 2
The test string "2" has length = 1
The characters are all the same
 
string to test: 333
The test string "333" has length = 3
The characters are all the same
 
string to test: .55
The test string ".55" has length = 3
Characters differ at position 2 ('5' = 35h)
 
string to test: tttTTT
The test string "tttTTT" has length = 6
Characters differ at position 4 ('T' = 54h)
 
string to test: 4444 444k
The test string "4444 444k" has length = 10
Characters differ at position 5 (' ' = 20h)
</pre>
 
 
=={{header|Scala}}==
18

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.