Strings and Runes in Fortran

! A Fortran string is a sequence of characters. The language
! and standard libraries treat strings as fixed-length arrays
! of characters. In Fortran, the concept of a character is 
! typically a single byte, which can represent ASCII characters.
! For Unicode support, additional libraries or techniques are needed.

program strings_and_characters
    use, intrinsic :: iso_fortran_env, only: int32
    implicit none

    ! 's' is a character string assigned a literal value
    ! representing the word "hello" in the Thai language.
    ! Note: Fortran doesn't natively support UTF-8 encoding,
    ! so we use ASCII representation here.
    character(len=*), parameter :: s = "sawadee"

    ! Print the length of the string
    print *, "Len:", len(s)

    ! Print the ASCII values of each character
    call print_ascii_values(s)

    ! Count the number of characters
    print *, "Character count:", len_trim(s)

    ! Iterate over each character in the string
    call print_characters(s)

    ! Demonstrate passing a character to a function
    call examine_character(s(1:1))

contains

    subroutine print_ascii_values(str)
        character(len=*), intent(in) :: str
        integer :: i

        do i = 1, len(str)
            write(*, '(Z2.2, 1X)', advance='no') iachar(str(i:i))
        end do
        print *
    end subroutine print_ascii_values

    subroutine print_characters(str)
        character(len=*), intent(in) :: str
        integer :: i

        print *, "Characters in string:"
        do i = 1, len(str)
            print *, "Character at", i, "is", str(i:i)
        end do
    end subroutine print_characters

    subroutine examine_character(c)
        character, intent(in) :: c

        if (c == 't') then
            print *, "found tee"
        else if (c == 's') then
            print *, "found ess"
        end if
    end subroutine examine_character

end program strings_and_characters

This Fortran program demonstrates basic string operations and character handling. Here’s a breakdown of the code:

  1. We define a string s with the value “sawadee” (a simplified ASCII representation of the Thai greeting).

  2. We print the length of the string using the len function.

  3. We use a subroutine print_ascii_values to print the hexadecimal ASCII values of each character in the string.

  4. We count the number of characters using len_trim, which is equivalent to the length for this string without trailing spaces.

  5. We iterate over each character in the string using a do loop in the print_characters subroutine.

  6. 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:

$ gfortran strings_and_characters.f90 -o strings_and_characters
$ ./strings_and_characters

This will compile the Fortran code and create an executable that you can run to see the output of the program.