This function receives a string and returns its Morse code translation. The input string can contain only alphabetic characters in upper or lower case, digits 0..9 and spaces. All other characters will be considered as spaces. Characters inside words are separated by one space, and words by two spaces.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string |
function string_to_morse(string) result(morse) character(len=*), intent(in) :: string character(len=:), allocatable :: morse character(len=1) :: c integer :: i, k morse = "" do i = 1, len_trim(string) c = string(i:i) select case (c) case ('A':'Z') k = iachar(c) - iachar('A') + 1 case ('a':'z') k = iachar(c) - iachar('a') + 1 case ('0':'9') k = iachar(c) - iachar('0') + 27 case default k = 0 ! A space end select if (k /= 0) then morse = morse // trim(morse_table(k)) // ' ' else morse = morse // ' ' end if end do morse = trim(morse) end function string_to_morse