Strings and Internal I/O
String Concat
Deferred Length
A deferred-length allocatable character(len=:) auto-resizes on assignment, perfect for building strings.
Program
Play the program to concatenate two parts with a space.
string_concat.f90
Replay: real traced execution (multi-file project)
program string_concat
implicit none
character(len=:), allocatable :: first, last, full
first = "Modern"
last = "Fortran"
full = first // " " // last
print '(A)', full
end program string_concat
first ← Modern
3character(len=:), allocatable :: first, last, full4first = "Modern"5last = "Fortran"values this stepModernfirstlast ← Fortran
4first = "Modern"5last = "Fortran"6full = first // " " // lastvalues this stepFortranlastfull ← Modern Fortran
5last = "Fortran"6full = first // " " // last7print '(A)', fullvalues this stepModern FortranfullModernfirstFortranlastprint '(A)', full
6 full = first // " " // last7 print '(A)', full8end program string_concatoutputModern Fortranvalues this stepModern Fortranfull
Follow the Join
firstholdsModern.lastholdsFortran.first // " " // lastjoins the first word, one space, and the last word.fullbecomesModern Fortran.- The program prints
Modern Fortran. | part | value | | --- | --- | |first| Modern | | spacer | one blank | |last| Fortran | |full| Modern Fortran |
deferred length
`character(len=:), allocatable` sizes the string on assign.
// concat
`a // b` joins two strings end-to-end.
auto-allocate
Assignment to an unallocated deferred-length string allocates the right length.
Exercise: string_concat.f90
Reproduce Modern Fortran, then trace how the two words and the one blank become the final string.