in reply to string matching/replacing

Splitting into an array simplifies this problem:
#!/usr/bin/perl -w use strict; use vars qw($str @s @f); $str = 'I am a brown icecube'; @s = split('',$str); @f = map {$_ eq ' ' ? ' ' : '*'} @s; while (<>) { my $guess=substr($_,0,1); foreach my $i (0..$#s) { if (lc $s[$i] eq lc $guess) { $f[$i] = $s[$i] } } print @f,"\n"; }