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

You want to download the contents of a url with perl. How would you do that?

0 Answers  


You want to empty an array. How would you do that?

0 Answers  


What is the difference between module and package?

4 Answers   EDS, IBM, Symantec,


What are the options that can be used to avoid logic errors in perl?

0 Answers  


How can you replace the characters from a string and save the number of replacements?

0 Answers  


What are the various file operations in perl. Explain with example.

0 Answers  


What is posix in perl?

0 Answers  


Where do we require ‘chomp’ and what does it mean?

0 Answers  


How will you get the count of parameters passed to a perl subroutine?

0 Answers  


Which guidelines by Perl modules must be followed?

0 Answers  


What is a perl references?

0 Answers  


What does the’$_’ symbol mean?

0 Answers  


Categories