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

后端 未结 3 2515
深忆病人
深忆病人 2021-02-19 21:11

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 retu

相关标签:
3条回答
  • 2021-02-19 21:25

    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.

    0 讨论(0)
  • 2021-02-19 21:38

    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.

    0 讨论(0)
  • 2021-02-19 21:47

    Wow -- even though I'd searched for over an hour, immediately after posting this, I found an answer here (under "Miscellaneous Fortran Hints and Tips").

    The code I used is as follows (for to_upper):

    function to_upper(strIn) result(strOut)
    ! Adapted from http://www.star.le.ac.uk/~cgp/fortran.html (25 May 2012)
    ! Original author: Clive Page
    
         implicit none
    
         character(len=*), intent(in) :: strIn
         character(len=len(strIn)) :: strOut
         integer :: i,j
    
         do i = 1, len(strIn)
              j = iachar(strIn(i:i))
              if (j>= iachar("a") .and. j<=iachar("z") ) then
                   strOut(i:i) = achar(iachar(strIn(i:i))-32)
              else
                   strOut(i:i) = strIn(i:i)
              end if
         end do
    
    end function to_upper
    

    Hope this helps somebody!

    0 讨论(0)
提交回复
热议问题