On 26/06/2013 10:32, Shlomi Fish wrote:

Hi,

  Could you please let me know how to reverse a string without using built
in function.

Do you want to avoid using the http://perldoc.perl.org/functions/reverse.html
built-in function or any built-in function whatsoever? Assuming the latter you
can do this:

[CODE]
#!/usr/bin/perl

use strict;
use warnings;

sub my_reverse
{
     my ($s) = @_;

     my $ret = "";

     for my $idx (0 .. length($s) - 1)
     {
         $ret = substr($s, $idx, 1) . $ret;
     }

     return $ret;
}

my $string = shift(@ARGV);

print "Reversed is:\n", my_reverse($string), "\n";
[/CODE]

Running it gives you:

[SHELL]

shlomif@telaviv1:~$ perl my-reverse.pl Hello
Reversed is:
olleH
shlomif@telaviv1:~$ perl my-reverse.pl Franklin
Reversed is:
nilknarF
shlomif@telaviv1:~$

[/SHELL]

I don't understand. You say you are providing a solution that avoids
"any built-in function whatsoever", and then use `length` and `substr`.

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to