#!/usr/bin/perl -w

# Prompt the user to enter at sentence (store in one variable) of at least 5 words.
print "Please enter a sentence of at least 5 words: ";
chomp($sent = <STDIN>);

# Break the string apart storing the individual words in an array.
@words = split(/\s+/, $sent);

# Print the original sentence to the screen with the first word capitalized and all others in lower case.
print ucfirst(lc($sent))."\n";

# Print the last 3 letters of each word in the sentence to the screen.
foreach $k (@words) {
    $piece = substr($k, length($k) -3, 3);
    print "$piece\n";
}

# Join the second and third words into a string and store in a new variable.
$joined = $words[1].$words[2];

# Print this new variable to the screen in all UPPER case letters except for the last letter
$offset = length($joined) -1;
$start = uc(substr($joined, 0, $offset));
$end = lc(substr($joined, $offset, 1));
print "The modified joined string is ".$start.$end.".\n";


    # Prompt the user to enter their full name
print "Please enter your first and last name: ";
chomp($name = <STDIN>);
 
    # Create a subroutine that returns a lower case string comprised of the capitalized entered last name and first initial as a single word (ie. "Mark Tucker" would be returned as "tuckerm") when passed the first and last name as arguments.
$user = &UserName($name);

    # Print this returned value to the screen.
print "Your username is $user.\n";

    # Prompt the user to enter the current day of the week.
print "Please enter the current day of the week: ";
chomp($weekday = <STDIN>);

    # Convert the day of the week to the first 3 letter in upper case.
$wday = uc(substr($weekday, 0, 3));

    # Print the weekday and the username to the screen with some descriptive text using one print statement.
print "The day of the week is $wday and your username is $user\n";
exit;


sub UserName {
    my $fullname = $_[0];
    my ($firstn, $lastn); # just declare the variables as local
    $fullname = lc($fullname);
    ($firstn, $lastn) = split(/\s+/, $fullname);
    my $finit = lc(substr($firstn, 0, 1));
    my $username = $lastn.$finit;
    return($username);
}