Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a to_upper() or to_lower() function in F90?

How does one write a (Intel) F90 function that converts a string into lowercase (or, alternatively, uppercase)? I want to pass a character array to the function and have it return a character array, e.g.

program main
    implicit none

    character*32 :: origStr = "Hello, World!"
    character*32 :: newStr

    newStr = to_lower(origStr)
    write (*,*) newStr

end program main

such that this program outputs hello, world!.

I've been starting with the to_lower() subroutine found at RosettaCode, but I can't figure out how to write it as a function.

Thanks in advance!

PS -- Bonus points if you can do it with a string of unfixed length!

like image 555
jvriesem Avatar asked May 25 '12 18:05

jvriesem


People also ask

What is toupper and tolower C++?

tolower() – toupper() — Convert Character Case The tolower() function converts the uppercase letter C to the corresponding lowercase letter. The toupper() function converts the lowercase letter c to the corresponding uppercase letter. Return Value. Both functions return the converted character.

Is there a toupper function in C?

In C, the toupper() function is used to convert lowercase alphabets to uppercase letters. When a lowercase alphabet is passed to the toupper() function it converts it to uppercase. When an uppercase alphabet is passed to the function it returns the same alphabet. Note: A ctype.

What is function of tolower in C++?

The tolower() function in C++ converts a given character to lowercase. It is defined in the cctype header file.

What does tolower () do to the character that you pass to it as a parameter?

The tolower() function takes an uppercase alphabet and convert it to a lowercase character. If the arguments passed to the tolower() function is other than an uppercase alphabet, it returns the same character that is passed to the function.


2 Answers

Here's one that doesn't rely on the ASCII representation

Pure Function to_upper (str) Result (string)

!   ==============================
!   Changes a string to upper case
!   ==============================

    Implicit None
    Character(*), Intent(In) :: str
    Character(LEN(str))      :: string

    Integer :: ic, i

    Character(26), Parameter :: cap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    Character(26), Parameter :: low = 'abcdefghijklmnopqrstuvwxyz'

!   Capitalize each letter if it is lowecase
    string = str
    do i = 1, LEN_TRIM(str)
        ic = INDEX(low, str(i:i))
        if (ic > 0) string(i:i) = cap(ic:ic)
    end do

End Function to_upper

You can easily change this to to_lower by switching the low and cap strings in the loop.

like image 88
SethMMorton Avatar answered Oct 30 '22 08:10

SethMMorton


As the original author of this code, I'm pleased that it's of some help. I used to wonder why these functions were not built in to Fortran. My guess is that they only work for a rather restricted set of letters, i.e. the ones used in English. If you have text in almost any other European language you will have characters with accents, and then converting them to upper or lower case is much harder. For example e-grave in French turned into upper case is usually shown as just plain E (the grave accent gets lost), but in e-acute it does not. The designers of Fortran have always tried to provide facilities which suit a wide range of languages, and doing upper/lower case conversion in a multi-language way is not at all easy. At least that's my guess as to why you have to do it yourself.

like image 28
Clive Page Avatar answered Oct 30 '22 06:10

Clive Page