Page 4 of 4 FirstFirst ... 234
Results 31 to 32 of 32

Thread: Beginner's Programming Challenge 18

  1. #31
    Join Date
    Apr 2008
    Beans
    286

    Re: Beginner's Programming Challenge 18

    It was very difficult to pick a winner, as all entries were good.

    And the winner is....

    ... red_Marvin with his entry in PERL.

    Code:
    #!/usr/bin/env perl                 # just tell bash that it is a perl program
    use warnings;                       # yell if I do something bad
    use strict;                         # be less permissive than default
    print "Enter key please:\n";        # text output
    my $line = <>;                      # define a scalar line, and read one line
                                        # from stdin into it
    my @k = split '', $line;             # define an array and split $line into
                                        # individual chars and store into it
    print "Enter message please:\n";
    my @m = split '', <>;               # shorter version of the above
    print "Result:\n";
    
    
    # start a for loop, the loop variable is $_ by default, and it will iterate
    # through the values 0 to the index of the last value in @m minus 1:
    # if we have the array @a, then $#a will be the index of the last item in @a
    # ( we included '\n' when we read from stdin before, but we don't want it in
    # the cipher, so for a string 'FOO\n' => ('F', 'O', 'O', '\n') we want to
    # get the chars at 0, 1, 2 == 0..$#m-1 !!
    
    for (0..$#m-1) {
    print chr(
            (
                (
                    ord(        # ord get's the ascii value of a char  
                        $m[$_]) # if we have the array @a $a[4] will get the item at position 4 in @a
                    +ord(
                        $k[$_%$#k]) # modulus with length of key, since keylength can be < messagelength
                    -130        # same as -65 -65 to make A<->0 B<->1 etc ('A' has 65 as ascii value)
                )       # now we have summed the numerical values of the message char and the right key char
                % 26    # to keep the value from overflowing
            ) +65       # to convert back so that A<->65 B<->66 etc
        );
    }
    print "\n";
    Take care,
    Rouslan

  2. #32
    Join Date
    Aug 2005
    Location
    Sweden
    Beans
    407

    Re: Beginner's Programming Challenge 18

    Thankyou, I have come up with a new challenge, see link:
    http://ubuntuforums.org/showthread.p...6#post10593716
    I hope it is hard, but not too hard.
    Good luck everyone!
    Don't peach linux. Melon it!

Page 4 of 4 FirstFirst ... 234

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •