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

no edit summary
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
No edit summary
Line 1,191:
"4444 444k" — length 9 — contains a different character at index 4: ' ' (0x20)
</pre>
=={{header|Fortran}}==
Basically, just a simple call to the intrinsic VERIFY() procedure.
Could also have been done with an INDEX() call in this simple case
where the set of characters to match is a single character. A
fancier format statement would eliminate the need for the HEX()
procedure.
<lang fortran>
program demo_verify
implicit none
call homogeneous('')
call homogeneous('2')
call homogeneous('333')
call homogeneous('.55')
call homogeneous('tttTTT')
call homogeneous('4444 444k')
contains
 
subroutine homogeneous(str)
character(len=*),intent(in) :: str
character(len=:),allocatable :: ch
character(len=*),parameter :: g='(*(g0))'
integer :: where
if(len(str)>0)then;ch=str(1:1);else;ch='';endif
where=verify(str,ch)
if(where.eq.0)then
write(*,g)'STR: "',str,'" LEN: ',len(str),'. All chars are a ','"'//ch//'"'
else
write(*,g)'STR: "',str,'" LEN: ',len(str), &
& '. Multiple chars found. First difference at position ',where, &
& ' where a ','"'//str(where:where)//'"(hex:',hex(str(where:where)),') was found.'
write(*,g)repeat(' ',where+5),'^'
endif
end subroutine homogeneous
 
function hex(ch) result(hexstr)
character(len=1),intent(in) :: ch
character(len=:),allocatable :: hexstr
hexstr=repeat(' ',100)
write(hexstr,'(Z0)')ch
hexstr=trim(hexstr)
end function hex
 
end program demo_verify
</lang>
{{out}}
<pre>
STR: "" LEN: 0. All chars are a ""
STR: "2" LEN: 1. All chars are a "2"
STR: "333" LEN: 3. All chars are a "3"
STR: ".55" LEN: 3. Multiple chars found. First difference at position 2 where a "5"(hex:35) was found.
^
STR: "tttTTT" LEN: 6. Multiple chars found. First difference at position 4 where a "T"(hex:54) was found.
^
STR: "4444 444k" LEN: 10. Multiple chars found. First difference at position 5 where a " "(hex:20) was found.
^
</pre>
 
 
 
 
=={{header|FreeBASIC}}==
Anonymous user