In: Computer Science
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.
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.