The title generator script is a script that when passed as string containing ser
ID: 3891307 • Letter: T
Question
The title generator script is a script that when passed as string containing series of words it will respond with a string where the words are properly capitalized as you would find in a title. The script should accept two arguments named Title and AllCaps. The title argument is where the user will pass a string to be properly capitalized. The AllCaps argument is a switch type argument that, if specified, the title will be returned in all capital letters rather than just the first character of each word. Here are some examples
Make-Title.ps1 “a tale of two cities” Returns: "A Tale of Two Cities"
Make-Title.ps1 “a tale of two cities” –AllCaps Returns: "A TALE OF TWO CITIES"
Make-Title.ps1 “a TALE of two cities” Returns: "A Tale of Two Cities"
Make-Title.ps1 “A TALE OF TWO CITIES” Returns: "A Tale of Two Cities"
Requirements
1) Create a script named <fname>_<lname>_Make-Title.ps1 (replace <fname> with your first name, and <lname> with your last name)
2) Script Accepts two arguments, Title and AllCaps
a) Title accepts a string of words that are to be turned into a title
b) AllCaps is a switch argument, if specified all the letters are capitalized
3) The script must contain a function named Proper that takes a single word and capitalizes the first letter and converts the remaining letters to lowercase.
4) The main script splits the string sent as the Title parameter into words then calls the Proper function to fix the case.
5) If the AllCaps switch is specified the original string is return as all capital letters
6) The script outputs a single string not an array of strings with all the words properly capitalized
7) The following words should only be capitalized if they are the first word of the title.
a) to, a, the, at, in, of, with, and, but, or, is
Hints
1) Look at the methods of a string “somestring”|get-member
2) -split
3) –join
4) –in or -notin (or –contains/ –notcontains) Deliverable
Explanation / Answer
#Here if allcaps is not null then all capital is returned and the code is written according to the conditions given in the question Enter the title and allcaps switch option with a comma in between them to get them as input. Hope this programs helps. Written in quite simple way
def proper(s):
s=s.lower()
s = s[0].upper()+s[1:]
return s
def final_output(title,allcaps):
if(allcaps!=''):
return title.upper()
title = title.lower()
arr = title.split(" ")
short_words = ['to', 'a', 'the', 'at', 'in', 'of', 'with', 'and', 'but', 'or', 'is']
final_string=''
count= 0
for word in arr:
if word in short_words and count is 0:
final_string = final_string + proper(word)+" "
elif word in short_words and count!=0:
final_string = final_string + word + " "
else:
final_string = final_string + proper(word)+" "
count = count+1
return final_string
title,allcaps= input().split(',')
final_output(title,allcaps)
print(final_output(title,allcaps))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.