Question

In: Computer Science

You must prompt the user to enter a menu selection. The menu will have the following...

You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line by itself. Display the length of each name on a line by itself. 2. ‘M’ – Middle Characters a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the middle character or the middle characters of each name in the file. Display each on a line by itself. 3. ‘R’ – Reverse a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display each name in the file in reverse to the screen. Display each on a line by itself. 4. If any menu selection other than ‘L, ‘M’, or ‘R’ is entered a. Display the message ‘You entered an invalid menu selection’ to the user.

Solutions

Expert Solution

Given, We need to open a file read all the content in it i.e; names either separated by white space or a new line character. We also need to perform the following operations based on the user's prompt. The user's prompt is case insensitive.

The operations are as follows:

1. 'L' or 'l' : Find and dispaly the length of each name in the file names.txt.

2. 'M' or 'm' : Find and display the middle character(s) of each name from the file names.txt.

3. 'R' or 'r' : Reverse and display each name from the file names.txt.

The programming langugae is not mentioned above. So, we use a high level programming language which can be easily understood. Let us consider the programming langugae to be python.

The algorithm is as follows:

Open the file names.txt present in your local machine.

Prompt the user to select the desired option from the menu.

If the option selected is present in the menu, perform the respective operations. Else prompt the user invalid menu selection.

For performing the operations, write 3 different functions and call them.

The text file names.txt is as follows:

john,ram,robert,rahim,abraham,clinton,christopher

The python program is as follows :


# Finding and returning the length of the name

def findLength(s):
    return len(s) # Returning the length od a string

# Finding and returning the middle character(s) from the name

def findMiddleCharacters(s):
    length = len(s)
    if length%2 == 0: # If length is even
        return s[length//2-1]+s[length//2] # Return two characters
    else:
        return s[length//2] # Return 1 character.

# Returning the reversed string

def reverseName(s):
    return s[::-1] # Returning the reverse of a string using slicing operation.

print('Select one of the options from below : ') 
print()

# options to be selected

print('L : LENGTH')
print()
print('M : MIDDLE')
print()
print('R : REVERSE')
print()
print('Enter the option : ', end = '')
f = open('F:/names.txt','r') # Opening the file containing the names separated by comma.
names  = f.readline().split(',') # Reading content from the file separated by using comma.
#print(names)
option  = input() # Taking input from the user.
if option == 'l' or option == 'L': # checking whether the option is l or L.
        for i in names:
                print(i, ':',end = ' ')
                print(findLength(i)) # Calling the function findLength
elif option == 'm' or option == 'M': # Checking whether the option is m or M.
    for i in names:
        print(i,':',end = ' ')
        print(findMiddleCharacters(i)) # Calling the function findMiddleCharatcers
elif option == 'r' or option == 'R': # Checking whether the option is r or R.
    for i in names:
        print(i,':',end = ' ')
        print(reverseName(i)) # Calling the function reverseName.
else:
    print('You entered an invalid menu selection') # If the option is not in the given menu list.

Output :

Testcase 1 :

Finding and returning length of each name:

Select one of the options from below :

L : LENGTH

M : MIDDLE

R : REVERSE

Enter the option : l
john : 4
ram : 3
robert : 6
rahim : 5
abraham : 7
clinton : 7
christopher : 11

Testcase 2 :

Finding and displaying the middle character(s):

Select one of the options from below :

L : LENGTH

M : MIDDLE

R : REVERSE

Enter the option : m
john : oh
ram : a
robert : be
rahim : h
abraham : a
clinton : n
christopher : t

Testcase 3:

Displaying the reverse of each name:

Select one of the options from below :

L : LENGTH

M : MIDDLE

R : REVERSE

Enter the option : r
john : nhoj
ram : mar
robert : trebor
rahim : mihar
abraham : maharba
clinton : notnilc
christopher : rehpotsirhc

Testcase 4:

Select one of the options from below :

L : LENGTH

M : MIDDLE

R : REVERSE

Enter the option : k
You entered an invalid menu selection

The screenshots are as follows:

I hope that this will give you all the details that you require and helps you learn the concepts as well.


Related Solutions

Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
(b) You will write a program that will do the following: prompt the user enter characters...
(b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘Q’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘Q’. Example input mJ0*5/]+x1@3qcxQ The ‘Q’ should be included when computing the statistics properties of the input. Since characters are integers...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT