The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 08-29-2008
email-lalit email-lalit is offline
Registered User
  
 

Join Date: May 2008
Posts: 36
Scramble a string.

Hi all,

I am trying to write a perl script that will take user input as a string and scramble the string and print the result.
Note: I cannot use shuffle function ....using for loop.


Code:
so here is example.
Enter a String: abcdef

Print the Result: debacf 

Enter a String: abcdef

print the Result: cdabef

Here is my perl code but it doesn't work exactly:

Code:
print "Enter a string:   ";
$sString = <STDIN>;

chomp($sString);

print "String = $sString\n";

$iLength = length($sString);
@array = split(//, $sString);

print "array = @array\n";

print "length = $iLength\n";
for ( $i = 1; $i <= $iLength; $i++ ){
$item = int(rand($#array));
print "@array[$item]\n";
}

Please any body help me.

thanks,