sort a word "system" in perl/shell without using built in
functions

output should be emssty

Answers were Sorted based on User's Feedback



sort a word "system" in perl/shell without using built in functions output should be e..

Answer / vipul dalwala

======= PERL SCRIPT (sortword.pl) =========

#!/usr/bin/perl

my $word = $ARGV[0];
$sortword = "";
$lastchar = "";

while($word =~ /(.)/g)
{
$lastchar = $1;
if( $sortword ) {
$flag = "";
$newsortword = "";
while($sortword =~ /(.)/g) {
if( $lastchar gt $1 || $flag
eq "charcovered") {

$newsortword =
$newsortword.$1;
$flag = "greater" if($flag
ne "charcovered")
}
else {
$newsortword =
$newsortword.$lastchar.$1;
$flag = "charcovered";
}
}
if( $flag ne "charcovered" ) {
$newsortword =
$newsortword.$lastchar;
}

$sortword = $newsortword;
}
else {
$sortword = $lastchar;
}
}

print $sortword."\n";


======= PERL SCRIPT =========

Run the script as:

sortword.pl "system"

Is This Answer Correct ?    1 Yes 0 No

sort a word "system" in perl/shell without using built in functions output should be e..

Answer / umesh

$value = "system";
@SortedArray = split('',$value);
$strlength = @SortedArray;
print "Current sort @SortedArray
";
$swapped = 0;
for($i=0; $i < $strlength; $i++){
for (my $j = 0; $j < $strlength-1-$i; $j++) {
if($SortedArray[$j] gt $SortedArray[$j+1]){
$tmp = $SortedArray[$j];
$SortedArray[$j] = $SortedArray[$j+1];
$SortedArray[$j+1] = $tmp;
$swapped =1;
}
}
if ($swapped) {
next;
}
}

print join('', split(' ', "@SortedArray")), "
";

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More CGI Perl Interview Questions

What is a chop() function in perl?

0 Answers  


Write a program to download the contents from www.perlinterview.com/answers.php website in Perl.

0 Answers  


Which has highest precedence in between list and terms? Explain?

0 Answers  


How can you define “my” variables scope in Perl and how it is different from “local” variable scope?

0 Answers  


Explain ivalue?

0 Answers  






What is the difference between having a parenthesis after module name and without parenthsis after module name?? i.e Package::Module(); and Package::Module;

1 Answers  


How to replace perl array elements?

0 Answers  


What is perl dbi?

0 Answers  


What is “grep” function in perl?

0 Answers  


What is caller function in perl?

1 Answers  


what are prefix dereferencer and list them out?

0 Answers  


Explain grooving and shortening of arrays and splicing of arrays?

0 Answers  


Categories