Logo

Programming-Idioms

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.
Implementation
Haskell

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Haskell implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
i := 0
for _, c := range s {
	fmt.Printf("Char %d is %c\n", i, c)
	i++
}
for i, c in enumerate(s):
    print(f'Char {i} is {c}')
lazutf8
for i := 1 to utf8length(s) do
  writeln(format('Char %d is %s',[i, utf8copy(s,i,1)]));
s.each_char.with_index{|c, i| puts "Char #{i} is #{c}" }
for (i, c) in s.chars().enumerate() {
    println!("Char {} is {}", i, c);
}
for (const [i, c] of [...s].entries()) {
	console.log(`Char ${i} is ${c}`);
}
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.forEachIndexed { i, c ->
  println("Char $i is $c")
}
s.split('').asMap().forEach((i, c) => print('Char $i is $c'));
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger i = new AtomicInteger(0);
s.chars().forEach(v -> {
  char c = (char)v;
  System.out.println("Char " + i.getAndIncrement() + " is " + c); 
});
for(int i=0; i<s.length(); i++) {
  char c = s.charAt(i);
  System.out.println("Char " + i + " is " + c); 
}
(dorun (map-indexed (fn [i c] (println "Char" i "is" c)) s))
use v5.10;
use utf8;
binmode STDOUT, ":utf8"; 

while ($s =~ /(\X)/g) {
    say 'Char ' . pos($s) . ' is ' . $1;
}
int i = 0;
for(char c : s.toCharArray()) {
	System.out.printf("Char %d is %c%n", i, c);
	i++;
}