! This file: http://ftp.aset.psu.edu/pub/ger/fortran/hdk/RemoveCh.f90 ! ! Example using standard conforming code.to remove occurence(s) of a ! character from a C string, s1 or a string, s2. ! ! Original posting and CLEN function by David Frank. ! This version of Remove_Ch by Dick Hendricson. ! as posted at comp.lang.fortran on 18 and 20 April 2006 PROGRAM varying_cstring integer :: n character(100) :: s1 = ' The qu*ick brown fox ju**mps '//char(0) character(100) :: s2 = ' The qu*ick brown fox ju**mps ' ! char(0) is the null character. ! s1, the C String case. n = Remove_Ch(s1,'*') write (*,*) n,' |',s1(1:n),'|' ! outputs 27 | The quick brown fox jumps | ! s2, the String case. n = Remove_Ch(s2,'*') write (*,*) n,' |',s2(1:n),'|' ! outputs 27 | The quick brown fox jumps | stop contains ! ---------------------- FUNCTION Remove_Ch(s,ch) RESULT (nc) character(*) :: s character :: ch, a(len(s)), v(len(s)) integer :: nc v = ' ' a = transfer(s,a) a = pack(a, a/=ch,v) ! 3rd argument forces shape conformance. s = transfer(a,s) nc = Clen(s) END FUNCTION ! ------------------------ PURE INTEGER FUNCTION Clen(s) ! get len of string (C or blank terminated) CHARACTER(*),INTENT(IN) :: s Clen = INDEX(s,char(0))-1 IF (Clen == -1) Clen = LEN_TRIM(s)+1 END FUNCTION Clen END PROGRAM