Determine if a string has all the same characters
- Task
Given a character string (which may be empty, or have a length of zero characters):
- create a function/procedure/routine to:
- determine if all the characters in the string are the same
- indicate if or which character is different from the previous character
- display each string and it's length (as the strings are being examined)
- a zero─length (empty) string shall be considered as all the same character(s)
- process the strings from left─to─right
- if all the same character, display a message saying such
- if not all the same character, then:
- display a message saying such
- display what character is different
- only the 1st different character need be displayed
- display where the different character is in the string
- the above messages can be part of a single message
- display the hexadecimal value of the different character
Use (at least) these seven test values (strings):
- a string of length 0 (an empty string)
- a string of length 3 which contains three blanks
- a string of length 1 which contains: 2
- a string of length 3 which contains: 333
- a string of length 3 which contains: .55
- a string of length 6 which contains: tttTTT
- a string of length 9 which a blank in the middle: 4444 444k
Show all output here on this page.
- Related task
REXX
<lang rexx>/*REXX program verifies that all characters in a string are all the same (character). */ @chr= ' [character' /* define a literal used for SAY.*/ @all= 'all the same character for string (length' /* " " " " " " */ @.= /*define a default for the @. array. */ parse arg x /*obtain optional argument from the CL.*/ if x\= then @.1= x /*if user specified an arg, use that. */
else do; @.1= /*use this null string if no arg given.*/ @.2= ' ' /* " " " " " " " */ @.3= 2 /* " " " " " " " */ @.4= 333 /* " " " " " " " */ @.5= .55 /* " " " " " " " */ @.6= 'tttTTT' /* " " " " " " " */ @.7= 4444 444k /* " " " " " " " */ end /* [↑] seventh value contains a blank.*/
do j=1; L= length(@.j) /*obtain the length of an array element*/ if j>1 & L==0 then leave /*if arg is null and J>1, then leave. */ r= allSame(@.j) /*R: ≡0, or the location of bad char.*/ if r\==0 then ?= substr(@.j,r,1) /*if not monolithic, obtain the char.*/ if r==0 then say ' ' @all L"):" @.j else say 'not' @all L"):" @.j @chr ? "('"c2x(?)"'x) at position" r"]." end /*j*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ allSame: procedure; parse arg y /*get a value from the argument list. */
if y== then return 0 /*if Y is null, then return 0 (zero)*/ return verify(y, left(y,1) ) /*All chars the same? Return 0 (zero)*/ /* else return location*/</lang>
- output when using the internal default inputs:
all the same character for string (length 0): all the same character for string (length 3): all the same character for string (length 1): 2 all the same character for string (length 3): 333 not all the same character for string (length 3): .55 [character 5 ('35'x) at position 2]. not all the same character for string (length 6): tttTTT [character T ('54'x) at position 4]. not all the same character for string (length 9): 4444 444K [character ('20'x) at position 5].