Strings and Runes in Co-array Fortran
This Co-array Fortran program demonstrates concepts similar to the Go example for strings and runes, adapted to Fortran’s capabilities:
We define a fixed-length string
s
with the value “Hello!”. In Fortran, strings are fixed-length character arrays.We print the length of the string using the
len
function.We iterate over each character in the string and print its ASCII value using the
iachar
function, which is similar to getting the byte values in Go.We count the number of characters using
len_trim
, which is roughly equivalent to counting runes in Go (for ASCII strings).We iterate over each character in the string, printing its value and position. This is similar to the range loop in Go, but adapted for Fortran’s string handling.
We demonstrate character comparison in the
examine_character
subroutine, which is similar to the rune comparison in Go.
To compile and run this program:
This will output:
Note that Co-array Fortran doesn’t have built-in UTF-8 support like Go, so this example uses ASCII characters. For proper Unicode handling in Fortran, you would need to use additional libraries or implement UTF-8 encoding/decoding yourself.