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

output should be emssty

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain goto name?

518


How to code in perl to implement the tail function in unix?

521


Perl regular expressions are greedy" what does this mean?

498


what is the function that is used to identify how many characters are there in a string?

534


What is warn function in perl?

530






What is cpan in perl?

548


What happens when you return a reference to a private variable?

526


what are the steps involved in reading a cgi script on the server?

493


Define print() function in perl?

551


What is hash?

555


What are the various perl data types based on the context?

535


What are the benefits of perl in using it as a web-based application?

491


Explain lexical variables.

515


What does `$result = f() .. g()' really return?

564


How many types of primary data structures in Perl and what do they mean?

845