Fortran无定义长度CHARACTER FUNCTION:编写通用F90字符串转大写函数
to_upper() Function in Fortran 90 Hey there! I get it—dealing with fixed-length strings in Fortran can be a real hassle, especially when you need flexibility for future changes. Let's walk through how to build a to_upper() function that works with any length of input string, no hardcoded limits required.
The Core Fix: Use Automatic-Length Dummy Arguments
Fortran 90 introduced support for automatic-length character arguments (character(len=*)), which lets your function accept strings of any length without predefining a maximum. We’ll pair this with a return string that matches the input’s length exactly to avoid truncation or extra padding.
Working Code Implementation
Here’s the full, flexible function:
function to_upper(input_str) result(upper_str) ! Accept any-length input string character(len=*), intent(in) :: input_str ! Return a string with the same length as the input character(len=len(input_str)) :: upper_str integer :: i, char_ascii ! Loop through each character in the input do i = 1, len(input_str) char_ascii = ichar(input_str(i:i)) ! Check if the character is a lowercase ASCII letter (a-z) if (char_ascii >= ichar('a') .and. char_ascii <= ichar('z')) then ! Convert to uppercase by subtracting 32 from the ASCII value upper_str(i:i) = char(char_ascii - 32) else ! Leave non-lowercase characters (spaces, punctuation, etc.) as-is upper_str(i:i) = input_str(i:i) end if end do end function to_upper
Key Details Breakdown
- Automatic-Length Input:
character(len=*)tells Fortran to adapt the dummy argument’s length to whatever string you pass in—no more hardcoding 32 or any other arbitrary number. - Matching Output Length:
character(len=len(input_str))ensures the returned string is exactly the same length as the input, so you won’t get unexpected truncation or trailing spaces. - ASCII Conversion: We use
ichar()to grab the ASCII code of each character, check if it falls in the lowercase range, then subtract 32 (the fixed difference between lowercase and uppercase ASCII values) to convert it. Non-letter characters stay unchanged.
How to Test the Function
Try it with both allocatable and fixed-length strings to confirm it works seamlessly:
program test_to_upper implicit none character(len=:), allocatable :: dynamic_str character(len=45) :: fixed_str ! Declare the function (or place it in a module for reuse) character(len=*) function to_upper(input_str) character(len=*), intent(in) :: input_str character(len=len(input_str)) :: to_upper integer :: i, char_ascii do i = 1, len(input_str) char_ascii = ichar(input_str(i:i)) if (char_ascii >= ichar('a') .and. char_ascii <= ichar('z')) then to_upper(i:i) = char(char_ascii - 32) else to_upper(i:i) = input_str(i:i) end if end do end function to_upper ! Test with an allocatable string dynamic_str = "Fortran 90 lets us avoid fixed limits!" print *, "Original: ", dynamic_str print *, "Uppercase: ", to_upper(dynamic_str) ! Test with a fixed-length string fixed_str = "this is a fixed-length test string for validation" print *, new_line('a')//"Original: ", fixed_str print *, "Uppercase: ", to_upper(fixed_str) end program test_to_upper
Quick Notes for Edge Cases
- If you need to handle non-ASCII characters (like accented letters), this basic ASCII-based method won’t work. For that, you’d need to use locale-aware functions or a custom lookup table for extended character sets.
- For better code organization, consider putting the
to_upper()function in a module—this avoids having to redeclare it in every program unit that uses it.
内容的提问来源于stack exchange,提问作者Bruno Fonseca

