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
Explain goto name?
How to code in perl to implement the tail function in unix?
Perl regular expressions are greedy" what does this mean?
what is the function that is used to identify how many characters are there in a string?
What is warn function in perl?
What is cpan in perl?
What happens when you return a reference to a private variable?
what are the steps involved in reading a cgi script on the server?
Define print() function in perl?
What is hash?
What are the various perl data types based on the context?
What are the benefits of perl in using it as a web-based application?
Explain lexical variables.
What does `$result = f() .. g()' really return?
How many types of primary data structures in Perl and what do they mean?