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!
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.
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.
The tolower() function in C++ converts a given character to lowercase. It is defined in the cctype header file.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With