Write a script to reverse a string without using Perl's
built in function
Answers were Sorted based on User's Feedback
Answer / kiruthikau
my $i;
my $str="hello";
my @str=split('',$str);
for($i=$#str;$i>=0;$i--)
{
print $str[$i];
}
| Is This Answer Correct ? | 47 Yes | 15 No |
Answer / vipul dalwala
#!/usr/bin/perl
my $str = "hello";
$revstr = "";
while($str =~ /(.)/g)
{
$revstr = $1.$revstr;
}
print $revstr."\n";
| Is This Answer Correct ? | 12 Yes | 2 No |
Answer / kiran pawar
my $string = "kiran";
my @name = split//,$string;
while(@name){
print pop @name;
}
| Is This Answer Correct ? | 9 Yes | 1 No |
Answer / arukumar g m
my $string="hello";
my $str;
my $i = length($string);
while($i>0) {
my $nthstr = substr($string, $i-1, 1);
$str.=$nthstr;
$i = $i-1;
}
print $str;
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / uma from sswdept - hov service
$strin = "uma";
print "\n \$strin <$strin>";
$len = length($strin);
@arr = split(//,$strin);
print "\n \$len $len \@arr <@arr>";
for($i=$len-1;$i>=0;$i--)
{
print $arr[$i];
$retVal .= join(//,$arr[$i]);
print "\n retVal <$retVal> ";
}
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / venkatesh
#!/usr/bin/perl
$a = "Venky";
@c;
@b = split("", $a);
foreach $char (@b){
unshift(@c, $char);
}
$value = join("", @c);
print "$value\n";
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / pallavi w
#!/usr/bin/perl -w
2
3 use strict;
4 my $string = "STRING";
5 my $rev_string = join "", reverse (split ("", $string));
6
7 print "The String is ". $string ."\n";
8 print "The reverse string is ". $rev_string ."\n";
| Is This Answer Correct ? | 4 Yes | 8 No |
How to know whether a key exists or not in perl?
How will you open a file in read-only mode in perl?
How many ways can we express string in Perl?
my @array=('data1','data2'); my @array1=('data1','data2'); my ($i,$k); $i=7; $k=7; while($i){ $array [++$#array] = 'ree'; $i--; print "@array"; } while($k){ push(@array1,'ree'); $k--; print "@array1"; } Are these two while loop are doing the same functionality ? What may be the difference?
Why does Perl not have overloaded functions?
How to do comment in perl?
How to read a single line from a file in perl?
How to print escaping characters inside a string in perl?
What are arrays in perl?
What elements of the Perl language could you use to structure your code to allow for maximum re-use and maximum readability?
How do find the length of an array?
What is subroutine in perl?