Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Fortran

Idiom #286 Iterate over characters of a string

Print a line "Char i is c" for each character c of the string s, where i is the character index of c in s (not the byte index).

Make sure that multi-byte characters are properly handled, and count for a single character.

use iso_fortran_env
  integer, parameter :: ucs4  = selected_char_kind ('ISO_10646')
  character(kind=ucs4,  len=30) :: s
open (output_unit, encoding='UTF-8')

do i=1,len(s)
  print *,"Char ", i, " is ", s(i:i)
end do

s has to be declared to be of the right kind for Unicode. The open statement makes sure that standard output is UTF-8.
(dorun (map-indexed (fn [i c] (println "Char" i "is" c)) s))

In Clojure a string is a list of characters

New implementation...