In: Computer Science
'''
Python version : 3.6
Python program to accept a string as input in which all of the
words are run together, but the first character of each word is
uppercase.
Convert the string to a string in which the words are separated by
spaces and only the first word starts with an uppercase
letter.
And then convert each word in the sentence to pig latin.
'''
# input the run together sentence
sentence = input('Enter the string: ')
# set resultant sentence to first character of sentence
format_sentence = sentence[0]
# set start_index to 1
start_index = 1
end_index = start_index # set end_index to start_index
# loop till end of string is reached
while end_index < len(sentence):
# end_index character is an upper-case letter
if sentence[end_index].isupper():
# add the sentence from start_index
to end_index to format_sentence followed by a space with the
lowercase end_index character to resultant string
format_sentence +=
sentence[start_index:end_index] + '
'+sentence[end_index].lower()
# update start_index to end_index +
1
start_index = end_index + 1
# increment end_index by 1
end_index += 1
# append the last word to the sentence
format_sentence += sentence[start_index:end_index]
# display the formatted sentence
print('Formatted sentence: ',format_sentence)
# Convert the format_sentence to Pig Latin
# strip is used to remove leading and trailing spaces
# split is used to split the input string using whitespace as
delimiter into list of strings
words = format_sentence.strip().split(" ")
pigLatin = "" # create an empty string for the resultant encrypted
string
# loop over the list of words, converting each word to pig_latin
and appending to encrypted followed by a space
for word in words:
# copy the word from second character to end of
word
# appended by first character of word followed by ay
and a space
pigLatin += word[1:]+word[0]+"ay "
# display the pig latin sentence
print('Pig Latin',pigLatin)
#end of program
Code Screenshot:
Output: