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

后端 未结 3 2503
深忆病人
深忆病人 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: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!

提交回复
热议问题