How can I replace aeiou with bfjpv in Perl?
I want to replace aeiou with bfjpv in user inputted string. Here is the code which is not working :-
print "Enter any String :";
while(($string = <>) ne "\n"){
@arr =开发者_如何学运维 split(//,$string);
for($i = 0 ; $i < $#arr ; $i++){
$char = $arr[$i];
if($char eq 'a' || $char eq 'e' || $char eq 'i' || $char eq 'o' || $char eq 'u'){
$char = $char + 1;
}
print $char;
}
}
What should I do to add one character? I mean how do I replace a
with b
.
You can just do
$char++;
in place of
$char = $char + 1;
Also you really don't need to loop to do the replacement. Just use the tr
operator as:
($new_string = $string) =~ tr [aeiou] [bfjpv];
$string =~ tr/aeiou/bfjpv/;
Does the whole job. See the perlop manual.
$char = chr(ord($char) + 1);
ord = convert character to integer
chr = convert integer to character
精彩评论