program main implicit none character*300::S character*300::SR integer::i,len,utf8len integer*1::code character::c S="" SR="" read *, S len = LEN_TRIM(S) i = 1 do if(i.gt.len) exit utf8len = get_utf8_len(S(i:i)) SR(len-i-utf8len+2:len-i+1) = S(i:i+utf8len-1) i = i + utf8len end do print '(a)', TRIM(SR) return contains logical function is_ascii(c) result(l) character::c l = .not.BTEST(ICHAR(c,1),7) end function is_ascii integer function get_utf8_len(c) result(len) character::c integer*1::ic ic = ICHAR(c,1) if(.not.BTEST(ic,7)) then len = 1 else len = LEADZ(NOT(ic)) end if end function get_utf8_len end program main