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

→‎{{header|S-BASIC}}: revamp output display
m (syntax highlighting fixup automation)
(→‎{{header|S-BASIC}}: revamp output display)
 
(19 intermediate revisions by 12 users not shown)
Line 445:
"4444 444k" OF LENGTH 9.
</pre>
 
==={{header|GWBASIC}}===
==={{header|Chipmunk Basic}}===
Works with BASICA
{{works with|Chipmunk Basic|3.6.4}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="gwbasic">
10 'SAVE"SAMECHAR", A
Line 905 ⟶ 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 915 ⟶ 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,134 ⟶ 1,141:
" " 0x20 is different at position 5
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func$ hex h .
for d in [ h div 16 h mod 16 ]
if d > 9
d += 7
.
h$ &= strchar (d + 48)
.
return h$
.
proc samechar s$ . .
s$[] = strchars s$
for i = 2 to len s$[]
if s$[i] <> s$[i - 1]
h = strcode s$[i]
write " --> different: '" & s$[i] & "' (" & hex h & "h)"
print "' position: " & i
return
.
.
print " --> ok"
.
repeat
s$ = input
until s$ = "EOF"
print "'" & s$ & "'" & " length " & len s$
samechar s$
print ""
.
input_data
 
2
333
.55
tttTTT
4444 444k
EOF
</syntaxhighlight>
 
 
=={{header|Erlang|Erlang}}==
<syntaxhighlight lang="erlang">
Line 1,280 ⟶ 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 1,564 ⟶ 1,644:
'tttTTT' -> inconsistent 'T' (0x54) at char 4
'4444 444' -> inconsistent ' ' (0x20) at char 5</pre>
 
=={{Header|Insitux}}==
 
{{Trans|Clojure}}
 
With a minor change from the Clojure entry - showing <code>^</code> underneath the first inconsistent character.
 
<syntaxhighlight lang="insitux>
(function check-all-chars-same string
(print "String (" string ") of len: " (len string))
(let num-same (count-while (= (0 string)) string))
(return-when (= num-same (len string))
(print "...all characters the same"))
(print (pad-left " " (+ 9 num-same) "^"))
(print "...character " num-same " differs - it is 0x"
(to-base 16 (char-code (num-same string)))))
 
(map check-all-chars-same ["" " " "2" "333" ".55" "tttTTT" "4444 444k"])
</syntaxhighlight>
 
{{out}}
 
<pre>
String () of len: 0
...all characters the same
String ( ) of len: 3
...all characters the same
String (2) of len: 1
...all characters the same
String (333) of len: 3
...all characters the same
String (.55) of len: 3
^
...character 1 differs - it is 0x35
String (tttTTT) of len: 6
^
...character 3 differs - it is 0x54
String (4444 444k) of len: 9
^
...character 4 differs - it is 0x20
</pre>
 
=={{header|J}}==
 
In J, <code>#~.y</code> gives a count of the unique items (characters) in y.
 
Here's a relatively concise (and grammatical) approach. For grammatical reasons we report the count of characters up through the differing character position, rather than its index:
Line 1,591 ⟶ 1,714:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.regex.Matcher;
import java.util.regex.Pattern;
</syntaxhighlight>
<syntaxhighlight lang="java">
public static void main(String[] args) {
String[] strings = {
"", " ", "2", "333", ".55", "tttTTT", "5", "4444 444k"
};
for (String string : strings)
System.out.println(printCompare(string));
}
 
static String printCompare(String string) {
String stringA = "'%s' %d".formatted(string, string.length());
Pattern pattern = Pattern.compile("(.)\\1*");
Matcher matcher = pattern.matcher(string);
StringBuilder stringB = new StringBuilder();
/* 'Matcher' works dynamically, so we'll have to denote a change */
boolean difference = false;
char character;
String newline = System.lineSeparator();
while (matcher.find()) {
if (matcher.start() != 0) {
character = matcher.group(1).charAt(0);
stringB.append(newline);
stringB.append(" Char '%s' (0x%x)".formatted(character, (int) character));
stringB.append(" @ index %d".formatted(matcher.start()));
difference = true;
}
}
if (!difference)
stringB.append(newline).append(" All characters are the same");
return stringA + stringB;
}
</syntaxhighlight>
<pre>
'' 0
All characters are the same
' ' 3
All characters are the same
'2' 1
All characters are the same
'333' 3
All characters are the same
'.55' 3
Char '5' (0x35) @ index 1
'tttTTT' 6
Char 'T' (0x54) @ index 3
'5' 1
All characters are the same
'4444 444k' 9
Char ' ' (0x20) @ index 4
Char '4' (0x34) @ index 5
Char 'k' (0x6b) @ index 8
</pre>
<br />
Alternate implementations
<syntaxhighlight lang="java">public class Main{
public static void main(String[] args){
Line 1,635 ⟶ 1,816:
</pre>
 
===Using Java 8===
<syntaxhighlight lang="java">
import java.util.OptionalInt;
 
public final class AllSameCharacters {
 
public static void main(String[] aArgs) {
String[] words = { "", " ", "2", "333", ".55", "tttTTT", "4444 444k" };
for ( String word : words ) {
analyse(word);
}
}
private static void analyse(String aText) {
System.out.println("Examining \"" + aText + "\", which has length " + aText.length() + ":");
 
OptionalInt mismatch = aText.chars().filter( ch -> ch != aText.charAt(0) ).findFirst();
if ( mismatch.isPresent() ) {
char fault = (char) mismatch.getAsInt();
System.out.println(" Not all characters in the string are the same.");
System.out.println(" Character \"" + fault + "\" (" + Integer.toHexString((char) fault)
+ ") is different at index " + aText.indexOf(fault));
} else {
System.out.println(" All characters in the string are the same.");
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Examining "", which has length 0:
All characters in the string are the same.
Examining " ", which has length 3:
All characters in the string are the same.
Examining "2", which has length 1:
All characters in the string are the same.
Examining "333", which has length 3:
All characters in the string are the same.
Examining ".55", which has length 3:
Not all characters in the string are the same.
Character "5" (35) is different at index 1
Examining "tttTTT", which has length 6:
Not all characters in the string are the same.
Character "T" (54) is different at index 3
Examining "4444 444k", which has length 9:
Not all characters in the string are the same.
Character " " (20) is different at index 4
</pre>
 
=={{header|JavaScript}}==
Line 3,124 ⟶ 3,355:
Input = '4444 444k', length = 9
First difference at position 5, character = ' '
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
<syntaxhighlight lang="RPL">
≪ DUP SIZE → s n
≪ "Examining [" s + "], length=" + n →STR + +
1 CF
IF n THEN
s 1 1 SUB 1
DO
s OVER DUP SUB
IF 3 PICK OVER ≠ THEN
1 SF
ROT DROP SWAP
ELSE DROP
END
1 +
UNTIL DUP n ≥ 1 FS? OR END
DROP
ELSE DUP
END
IF 1 FC? THEN
DROP
"All chars are the same."
ELSE
"'" OVER +
"' (hex=" + OVER NUM HEX R→B →STR +
") is different at pos " + s 4 ROLL POS →STR +
END
'ALLSAME?' STO
≪ { "" " " "2" "333" ".55" "tttTTT" "4444 444k" }
→ tl ≪
1 tl SIZE FOR j
tl j GET ALLSAME?
NEXT
EVAL
</syntaxhighlight>
{{out}}
<pre>
14:"Examining [], length=0"
13:"All chars are the same"
12:"Examining [ ], length=3"
11:"All chars are the same"
10:"Examining [2], length=1"
9:"All chars are the same"
8:"Examining [333], length=3"
7:"All chars are the same"
6:"Examining [.55], length=3"
5:"'5' (hex=# 35h) is different at pos 2"
4:"Examining [tttTTT], length=6"
3:"'T' (hex=# 54h) is different at pos 4"
2:"Examining [4444 4444k], length=9"
1:"' ' (hex=# 20h) is different at pos 5"
</pre>
 
Line 3,203 ⟶ 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,537 ⟶ 3,895:
' ' (0x20) is different at position 4</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn analyze(s string) {
chars := s.runes()
le := chars.len
Line 3,614 ⟶ 3,972:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var analyze = Fn.new { |s|
18

edits