Strings and Runes in Fortran
This Fortran program demonstrates basic string operations and character handling. Here’s a breakdown of the code:
We define a string
s
with the value “sawadee” (a simplified ASCII representation of the Thai greeting).We print the length of the string using the
len
function.We use a subroutine
print_ascii_values
to print the hexadecimal ASCII values of each character in the string.We count the number of characters using
len_trim
, which is equivalent to the length for this string without trailing spaces.We iterate over each character in the string using a do loop in the
print_characters
subroutine.We demonstrate passing a character to a function with
examine_character
.
Note that Fortran doesn’t have built-in support for Unicode or UTF-8 encoding. For proper handling of non-ASCII characters, you would need to use external libraries or more advanced techniques.
To compile and run this Fortran program:
This will compile the Fortran code and create an executable that you can run to see the output of the program.