RosettaCodeData/Task/Determine-if-a-string-has-all-the-same-characters/Mathematica/determine-if-a-string-has-all-the-same-characters.math
2023-07-01 13:44:08 -04:00

25 lines
794 B
Text

ClearAll[AllSameCharacters]
AllSameCharacters[s_String] := Module[{c = Characters[s], i = 0, tf},
If[Length[c] > 1,
tf = AllTrue[Rest[c], (i++; # == First[c]) &];
If[tf,
Print["input = \"", s, "\", Length = ", StringLength[s],
", All the same!"]
,
Print["input = \"", s, "\", Length = ", StringLength[s],
", Character " <> ToString[i + 1] <>
" is different from character " <> ToString[i], ", hex = ",
IntegerString[First@ToCharacterCode[c[[i + 1]]], 16]]
]
,
Print["input = \"", s, "\", Length = ", StringLength[s],
", All the same!"]
];
]
AllSameCharacters[""]
AllSameCharacters[" "]
AllSameCharacters["2"]
AllSameCharacters["333"]
AllSameCharacters[".55"]
AllSameCharacters["tttTTT"]
AllSameCharacters["4444 444k"]