Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Character case conversion

2 views
Skip to first unread message

Kenny Stultz

unread,
Oct 24, 2007, 4:41:40 PM10/24/07
to
I have a rookie question.

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

Paul van Delst

unread,
Oct 24, 2007, 5:16:11 PM10/24/07
to

"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

Colin Watters

unread,
Oct 24, 2007, 6:23:39 PM10/24/07
to

"Kenny Stultz" <stu...@wai.com> wrote in message
news:5o9p04F...@mid.individual.net...

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


Terence

unread,
Oct 24, 2007, 7:17:12 PM10/24/07
to
There are other ways.
The best general case is to locate the upper-lower case table of the
language you are using (via a service or API call).
This is a two-way look-up table, indexed by the ascii value of the
character (up or down as opposite half of the index character pair (as
16-bits words). The same can be even more generalised to 16-bit
(bouble byte) international character sets.

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.

James Tursa

unread,
Oct 24, 2007, 10:22:05 PM10/24/07
to

Your version assumes a default collating sequence that is nice and
consecutive. To force this in all cases, use iachar and achar instead
of ichar and char. This forces the ASCII collating sequence. (On many
compiler/machines the two versions are the same, but this is not
always the case). e.g.,

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


0 new messages