I have a character string. It may contain upper case or lower case characters.
How can I convert it to a character strign that has only upper case characters.
For example, I'd like to take 'ReStart12-3Now' and end up with
'RESTART12-3NOW'.
I can't seem to find a simple way to do it.
Thanks, Kenny
"Simple" is a relative term..... :o)
The following module snippet is what I use (shamelessly cribbed from Cooper Redwine's
"Upgrading to Fortran90"). It should ignore non-alpha characters.
MODULE String_Utility
IMPLICIT NONE
PRIVATE
PUBLIC :: StrUpCase, StrDownCase
CHARACTER(*), PARAMETER :: LOWER_CASE = 'abcdefghijklmnopqrstuvwxyz'
CHARACTER(*), PARAMETER :: UPPER_CASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CONTAINS
FUNCTION StrUpCase ( Input_String ) RESULT ( Output_String )
CHARACTER(*), INTENT(IN) :: Input_String
CHARACTER(LEN(Input_String)) :: Output_String
! Local variables
INTEGER :: i, n
! Copy input string
Output_String = Input_String
! Loop over string elements
DO i = 1, LEN(Output_String)
! Find location of letter in lower case constant string
n = INDEX(LOWER_CASE, Output_String( i:i ))
! If current substring is a lower case letter, make it upper case
IF ( n /= 0 ) Output_String( i:i ) = UPPER_CASE( n:n )
END DO
END FUNCTION StrUpCase
FUNCTION StrDownCase ( Input_String ) RESULT ( Output_String )
...same deal but opposite....
END FUNCTION StrDownCase
END MODULE String_Utility
EXAMPLE:
string = 'this is a string'
WRITE( *,'(a)' ) StrUpCase( string )
THIS IS A STRING
cheers,
paulv
I have used this for as long as I can remember. It has an important
assumption in it that Paul's doesn't suffer from.
SUBROUTINE LTOU ( WORD )
character*(*) word
integer i,j
integer, parameter :: a = ichar('a')
integer, parameter :: z = ichar('z')
integer, parameter :: dd = ichar('z') - ichar('Z')
DO I=1,len_trim(word)
j=ichar(word(i:i))
if(j.ge.a .and. j.le.z) word(i:i) = char(j-dd)
enddo
RETURN
END
--
Qolin
Email: my qname at domain dot com
Domain: qomputing
Another way is to create you own unique (perhaps) table and just run
through the text string indexing out the translation.
It is usual to note the presnce of a full-stop and retain upper case
as the next non-blank symbol even if translating to lower case, if
this is for converting written text and not to get lower-case fortran
source code from a more usual upper-case F77 and previous source
files.
SUBROUTINE LTOU ( WORD )
character*(*) word
integer i,j
integer, parameter :: a = iachar('a')
integer, parameter :: z = iachar('z')
integer, parameter :: dd = iachar('z') - iachar('Z')
do i=1,len(word)
j = iachar(word(i:i))
if( j.ge.a .and. j.le.z ) word(i:i) = achar(j-dd)
enddo
RETURN
END