3

Within Fortran, what are the library functions, if any (I'm using gfortran 11) that return whether the character is upper or lower case?

character :: c
c = 'l'
print *, is_lower(c) ! should display 'T'
print *, is_upper(c) ! should display 'F'
c = 'L'
print *, is_upper(c) ! should display 'T'

What should I be using in place of is_lower and is_upper? I can try to roll my own, but the comparison operators are weird enough with characters that I can't be sure of exactly what I'm doing.

rigel
  • 485
  • 1
  • 6
  • 12
  • 1
    You can probably adapt the code in https://stackoverflow.com/questions/63339506/case-insensitive-string-comparisons-in-fortran/63346503#63346503 to your needs without too much difficulty. One point to bear in mind is the `kind` of any characters you wish to check. – High Performance Mark Aug 12 '22 at 08:44
  • 1
    It really depends on what kind of characters are allowed and what kind of string it is. An ASCII string? UTF8 string? UTF32(USC4) string? What about letters like ß, are they allowed? What about accented letters? What about other alphabets (e.g. Georgian კარგი). Truly general solutions require specialized libraries. With just plain ASCII it is trivial, you can just check the ASCII code intervals. – Vladimir F Героям слава Aug 13 '22 at 10:05

2 Answers2

2

Thanks to @High Performance Mark's help, I have this hacky way of doing the checking for each character:

module char_cases
  implicit none

  private
  public :: is_upper_ascii, is_lower_ascii

  character(len=26), parameter :: uca = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  character(len=26), parameter :: lca = 'abcdefghijklmnopqrstuvwxyz'

contains

  pure elemental logical function is_upper_ascii(ch)
    character, intent(in) :: ch
    is_upper_ascii = index(uca, ch) /= 0
  end function is_upper_ascii

  pure elemental logical function is_lower_ascii(ch)
    character, intent(in) :: ch
    is_lower_ascii = index(lca, ch) /= 0
  end function is_lower_ascii

end module char_cases
rigel
  • 485
  • 1
  • 6
  • 12
1

You could use an ascii comparison... trasform c with iachar in integer and compare it with the boundaries of uppercase characters (from 65 to 90) or with the boundaries of lowercase characters (from 97 to 122) that is limited solution because it will not take care of strange characters that are uppercase or lowercase like ÈÉ

Gicu Aftene
  • 502
  • 2
  • 9