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

Novice question: Simplest way to convert string to all Uppor or Lower Case characters?

1 view
Skip to first unread message

Kelvin Hales

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
Simplest way to convert string to all Upper or Lower Case characters?
I was surprised to find (after much searching) that DVF 5.0 seems not
to include any function to carry out this simple task.

Ideas please?

Kelvin Hales

Markku Save

unread,
May 6, 1999, 3:00:00 AM5/6/99
to

Kelvin Hales wrote in message ...

Try something like this:
diff=ichar('A')-ichar('a')
upper=char(ichar(lower)+diff)

Markku Save


Kurt Kaellblad

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
In article <VA.0000002...@khales.cix.co.uk>, kha...@khales.cix.co.uk wrote:
>Simplest way to convert string to all Upper or Lower Case characters?
>I was surprised to find (after much searching) that DVF 5.0 seems not
>to include any function to carry out this simple task.
>
>Ideas please?
>
>Kelvin Hales
>

There are CHAR/ICHAR and ACHAR/IACHAR which can be used.
But I don't want to start a new thread about these functions!

My need for this type of conversions is very limited and I don't care about
what time it takes. Furthermore I often want to deal with "odd" european
characters. Thus I use the module below which is easy to extend for
different local letters.

Hope this helps, Kurt

!==============================
module up_lw

character(len=26), parameter, private :: &
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", &
lower = "abcdefghijklmnopqrstuvwxyz"

contains

subroutine lower_case( str )
implicit none
integer :: i, is
character(len=*) str
!-------------------------------
do is = 1, len_trim( str )
i = index( upper, str(is:is) )
if( i .gt. 0 ) str(is:is) = lower(i:i)
enddo
end subroutine lower_case

subroutine upper_case( str )
implicit none
integer :: i, is
character(len=*) str
!-------------------------------
do is = 1, len_trim( str )
i = index( lower, str(is:is) )
if( i .gt. 0 ) str(is:is) = upper(i:i)
enddo
end subroutine upper_case

end module up_lw
!==============================

Kurt Kaellblad, Ph.D. <kurt.k...@bkl.lth.se>
Building Science, Lund Institute of Technology
Lund University, Sweden


James Giles

unread,
May 6, 1999, 3:00:00 AM5/6/99
to

Markku Save wrote in message <7grvsc$fig$1...@news.kolumbus.fi>...
...

>Try something like this:
>diff=ichar('A')-ichar('a')
>upper=char(ichar(lower)+diff)

That works, provided that the character 'lower' is guaranteed to be
alphabetic and provided that all the alphabetic characters have the same
fixed difference in the collating sequence (not necesarily a portable
assumption).

--
J. Giles

Herman D. Knoble

unread,
May 6, 1999, 3:00:00 AM5/6/99
to
DV6 has UCASE and LCASE Character functions.
Don't know about DV5.

On Thu, 06 May 1999 11:33:48 +0100, Kelvin Hales <kha...@khales.cix.co.uk>
wrote:

-|Simplest way to convert string to all Upper or Lower Case characters?
-|I was surprised to find (after much searching) that DVF 5.0 seems not
-|to include any function to carry out this simple task.
-|
-|Ideas please?
-|
-|Kelvin Hales
-|


Herman D. Knoble, research associate
mailto:h...@psu.edu
http://www.personal.psu.edu/hdk
Penn State Center for Academic Computing
214 C Computer Building
University Park, PA 16802-2101
phone: +1 (814) 865-0818
fax: +1 (814) 863-7049
Penn State Center for Academic Computing
214-C Computer Building
University Park, PA 16802-2101
phone: +1 (814) 865-0818
fax: +1 (814) 863-7049


Kurt Kaellblad

unread,
May 7, 1999, 3:00:00 AM5/7/99
to
In article <3731ea43...@news.cac.psu.edu>, h...@psu.edu wrote:
>DV6 has UCASE and LCASE Character functions.
>Don't know about DV5.
>

Where did you find these functions???

I'm using DVF6.0B and the only place I can find UCASE and LCASE is in the
online documentation but only as functions in MS's Visual Basic. Any attempt
to link a Fortran program with these functions result in "error LNK2001:
unresolved external symbol".

Greetings, Kurt

Paul Zarchan

unread,
May 12, 1999, 3:00:00 AM5/12/99
to

> Simplest way to convert string to all Upper or Lower Case characters?

> I was surprised to find (after much searching) that DVF 5.0 seems not

> to include any function to carry out this simple task.
>

> Ideas please?
>
> Kelvin Hales

Here is a simple f77 program which will convert a file to upper case

IMPLICIT NONE

CHARACTER*20 Fname
CHARACTER*80 LineIn, LineOut
INTEGER*4 j,ich

write(9,*) 'Enter filename'
read (9,*) Fname

open(2,file=Fname)
open(3,file='uc'//Fname)


DO
LineIn = ' '
LineOut = ' '
read(2,'(a)',END=100) LineIn

DO (j=1,80)
ich = ichar( LineIn(j:j) )
if(ich .ge. 97 .and. ich .le. 122) ich = ich - 32

LineOut(j:j) = char(ich)
END DO

write(3,'(a)') LineOut


END DO
100 CONTINUE

STOP
END

Paul Zarchan

Gerry Thomas

unread,
May 12, 1999, 3:00:00 AM5/12/99
to

Kurt Kaellblad wrote in message <7gu6p6$7fo$1...@news.lth.se>...

>In article <3731ea43...@news.cac.psu.edu>, h...@psu.edu wrote:
>>DV6 has UCASE and LCASE Character functions.
>>Don't know about DV5.
>>
>
>Where did you find these functions???
>
>I'm using DVF6.0B and the only place I can find UCASE and LCASE is in the
>online documentation but only as functions in MS's Visual Basic. Any
attempt
>to link a Fortran program with these functions result in "error LNK2001:
>unresolved external symbol".
>


Use DVF 6's user32 mod; it has interfaces to the API's CharLower/Upper.

Regards,
Gerry T.

Jim Klein

unread,
May 12, 1999, 3:00:00 AM5/12/99
to
Kelvin Hales <kha...@khales.cix.co.uk> wrote:

>Simplest way to convert string to all Upper or Lower Case characters?
>I was surprised to find (after much searching) that DVF 5.0 seems not
>to include any function to carry out this simple task.

>Ideas please?

>Kelvin Hales

This subroutine makes lower case letters in the character variable all
upper case.

SUBROUTINE UPPER(STRING)
IMPLICIT NONE
CHARACTER STRING*140
INTEGER I,J
DO I=1,140
J=ICHAR(STRING(I:I))
IF(J.GE.97.AND.J.LE.122) STRING(I:I)=CHAR(J-32)
END DO
RETURN
END

Kelvin Hales

unread,
May 13, 1999, 3:00:00 AM5/13/99
to
Thanks for all the ideas and replies to date, here and by email. This
(simple) question certainly sparked a lot of comment and debate (in a
later thread).

Kelvin


Paul Shirron

unread,
May 13, 1999, 3:00:00 AM5/13/99
to
Jim Klein wrote:

This doesn't work for s..t on the mainframe IBM with EBCDIC codes.

James Giles

unread,
May 13, 1999, 3:00:00 AM5/13/99
to

Paul Shirron wrote in message <373AC05C...@boeing.com>...
>Jim Klein wrote:
...

>> This subroutine makes lower case letters in the character variable all
>> upper case.
>>
>> SUBROUTINE UPPER(STRING)
>> IMPLICIT NONE
>> CHARACTER STRING*140
>> INTEGER I,J
>> DO I=1,140
>> J=ICHAR(STRING(I:I))
>> IF(J.GE.97.AND.J.LE.122) STRING(I:I)=CHAR(J-32)
>> END DO
>> RETURN
>> END
>
>This doesn't work for s..t on the mainframe IBM with EBCDIC codes.

It doesn't even work on PC clones running windows if you're using the
whole extended character set. The conversion of å to Å won't be done.
And the letters š and Š are not even 32 units apart in the collating sequence.
(If you can't read the examples given here, you obviously don't have the
same character set - a demonstration of the fact that this question is
implementation dependent.)

Case conversion is inherently system dependent (unless you're using IACHAR
and ACHAR and limiting yourself to pure ASCII). A faster means is to do the
following:

Subroutine UPPER (string)
implicit none
Character string*140
Integer i, j
Integer, parameter :: Cmax = size_of_character_set
Character :: table(Cmax) = (/ initialize array of character*1 &
consisting of the whole character set &
in collating sequence order, but with &
upper-case letters where the lower-case &
would normally be /)
Do i = 1,140
j = ICHAR (string(i:i))
string(i:i) = table(j)
End do

End Subroutine UPPER

This is not portable (but then, assumptions about which characters are lower-
and which are upper-case aren't portable anyway). It is fast however. A smart
compiler may eve be able to recognize that the whole body of the loop is just
a single XLAT instruction (if the hardware has such a thing, or something
corresponding).

--
J. Giles

0 new messages