Determine if a string is squeezable: Difference between revisions

no edit summary
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
No edit summary
Line 1,455:
begin
if (i = 1) or (s[i - 1] <> s[i]) or ((s[i - 1] = s[i]) and (s[i] <> include)) then
sb.Append(s[i]);
end;
Result := sb.ToString;
Line 1,604:
After squeeze: ««« --- Hary S Truman »»» (length 71)
</pre>
 
=={{header|Fortran}}==
<lang fortran>
program main
implicit none
character(len=:),allocatable :: strings(:)
 
strings=[ character(len=72) :: &
'', &
'"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln', &
'..1111111111111111111111111111111111111111111111111111111111111117777888', &
'I never give ''em hell, I just tell the truth, and they think it''s hell.',&
' --- Harry S Truman' &
]
 
call printme( trim(strings(1)), ' ' )
call printme( strings(2:4), ['-','7','.'] )
call printme( strings(5), [' ','-','r'] )
 
contains
 
impure elemental subroutine printme(str,chr)
character(len=*),intent(in) :: str
character(len=1),intent(in) :: chr
character(len=:),allocatable :: answer
write(*,'(a)')repeat('=',9)
write(*,'("IN: <<<",g0,">>>")')str
answer=compact(str,chr)
write(*,'("OUT: <<<",g0,">>>")')answer
write(*,'("LENS: ",*(g0,1x))')"from",len(str),"to",len(answer),"for a change of",len(str)-len(answer)
write(*,'("CHAR: ",g0)')chr
end subroutine printme
 
elemental function compact(str,charp) result (outstr)
 
character(len=*),intent(in) :: str
character(len=1),intent(in) :: charp
character(len=:),allocatable :: outstr
character(len=1) :: ch, last_one
integer :: i, pio ! position in output
 
outstr=repeat(' ',len(str)) ! start with a string big enough to hold any output
if(len(outstr)==0)return ! handle edge condition
last_one=str(1:1) ! since at least this long start output with first character
outstr(1:1)=last_one
pio=1
 
do i=2,len(str)
ch=str(i:i)
pio=pio+merge(0,1, ch.eq.last_one.and.ch.eq.charp) ! decide whether to advance before saving
outstr(pio:pio)=ch ! store new one or overlay the duplcation
last_one=ch
enddo
 
outstr=outstr(:pio) ! trim the output string to just what was set
end function compact
 
end program main
}</lang>
{{out}}
<pre>
=========
IN: <<<>>>
OUT: <<<>>>
LENS: from 0 to 0 for a change of 0
CHAR:
=========
IN: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
OUT: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
LENS: from 72 to 70 for a change of 2
CHAR: -
=========
IN: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
OUT: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
LENS: from 72 to 69 for a change of 3
CHAR: 7
=========
IN: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
OUT: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
LENS: from 72 to 72 for a change of 0
CHAR: .
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< --- Harry S Truman >>>
LENS: from 72 to 20 for a change of 52
CHAR:
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< - Harry S Truman >>>
LENS: from 72 to 70 for a change of 2
CHAR: -
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< --- Hary S Truman >>>
LENS: from 72 to 71 for a change of 1
CHAR: r
</pre>
 
 
 
 
=={{header|FreeBASIC}}==
Anonymous user