Question

In: Computer Science

Using Perl; Part 1: Allow the user to enter a full name in the “first last”...

Using Perl;

Part 1:
Allow the user to enter a full name in the “first last” format
Print just the first name
Print just the last name
Print the name in “last, first” format
Print the entire name out in all capital letters
Use a single print statement to print out the first name on one line and the last name on
the next line.
There should be 5 print statements generating 6 lines of output.


Part 2:
Enter a three digit number (such as 316)
Print out the number from the hundreds place (for example, 3 in 316)
Print out the number from the ten’s place (for example, 1 in 316)
Print out the number from the one’s place (for example, 6 in 316)
Print out the original number
All output will be appropriately labeled and on a separate line, such as “hundreds
place”, “tens place”, and so on.
ALL the numeric values MUST be right justified in the output using formatted output
All work done for this part MUST treat the information as numeric values – do not treat
the input as a string or as an array.
There should be 4 print statements generating 4 lines of output.

Solutions

Expert Solution

Solution for Part1:

  1. First get the user input by prompting user for entering full name.
  2. Found the index of space character.
  3. Use the index position to get first name and last name using substr function
  4. in last statement \n is used to print first name and last name on separate lines using single print statement.

Code:

# allow the user to enter full name
print "Enter full name in format. First Last: ";
chomp($full_name = <>);

#Print just the first name
$index = index($full_name, ' ');
$first_name = substr($full_name, 0, $index);
print "$first_name\n";

#Print just the last name
$last_name = substr($full_name, $index + 1);
print "$last_name\n";

#Print the name in “last, first” format
print "$last_name, $first_name\n";

#Print the entire name out in all capital letters
$full_name_in_caps = "\U$full_name";
print "$full_name_in_caps\n";

#Use a single print statement to print out the first name on one line and the
#last name on the next line.
print "$first_name\n$last_name";

Output:

Solution for Part 2:

First get the user input. Use modulus operator to get the one's place digit. divide the number with 10 to get quotient. Now again perform modulus operation to get tens' place. divide the number with 10 to gt quotient. The quotient is the hundreds place digit. Use sprintf function to format the output. 3 places ("%3d") are used to right justify the output.

Code:

#Enter a three digit number (such as 316)
print "Enter a 3 digit number: ";
chomp($num = <>);

# make an copy of original number
$num2 = $num;

#evaluate the ones place using modulus % operator
#spritf used to format the output to right justify 3 places
$ones_place = sprintf("%3d",$num2 % 10);

#divide the num2 with 10 so that we can get quotent
$num2 = $num2 / 10;
$tens_place = sprintf("%3d",$num2 % 10);

#divide the num2 with 10 so that we can get quotent
$num2 = $num2 / 10;

# no need to use modulus operator here as we are left with single digit only in num2
$hundreds_place = sprintf("%3d",$num2);

#Print out the number from the hundreds place (for example, 3 in 316)
print "Hundreds place:\t\t$hundreds_place\n";

#Print out the number from the ten’s place (for example, 1 in 316)
print "Tens place:\t\t$tens_place\n";

#Print out the number from the one’s place (for example, 6 in 316)
print "Ones place:\t\t$ones_place\n";

#Print out the original number
print "Original number:\t$num";

Output:


Related Solutions

Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
Return the full names (first and last) of actors with “SON” in their last name, ordered...
Return the full names (first and last) of actors with “SON” in their last name, ordered by their first name. Find all the addresses where the second address (i.e. address2) is not empty (i.e., contains some text), and return these second addresses sorted. Your Professor wants you to find all the information from rental table associated with those staff (or staff ID) whose address ID is 4 (in staff table). Professor wants you to use Sub-query function. Your Professor wants...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
First, the Python program prompts user to enter user information (name, email, and phone number). Then...
First, the Python program prompts user to enter user information (name, email, and phone number). Then it displays a menu called “Fish Information” that has the following fish type: 1. Cat Fish 2. Red Fish 3. Any other fish Let user choose the fish type that he/she got and input the length of the fish. Then the program will determine what should be done with this particular fish. Based on the following criteria: Criteria: Length: FISHTYPE - Cat Fish <10:...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT