In: Computer Science
Write a program to perform the following two tasks:
1. The program will 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. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string.
2. Then the program will convert each word in the result string of task 1 into "Pig Latin" and display the result string. In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word.
For example, for the result string "Stop and smell the roses" in task 1, the Pig Latin string should be "topSay ndaay mellsay hetay osesray"
Hints:
1. In the first task, the first letter of a sentence should always be uppercase. Starting from the second letter to the last letter, if the letter is an uppercase (be cautious with I), that means it is the beginning of a new word. What you need to do is to insert a space before that uppercase letter, and change that uppercase letter to lowercase.
Python has isupper() function to check if a string or a character is uppercase, islower to check if a string or a character is lowercase
you may need a variable to remember the beginning index of a word and the ending index of a word. So you can use the two indexes to extract the word from the input phrase.
to add a space after a word, you can use the + operator as long as both operands are strings/characters
2. In the second task (pig Latin task), the result string of the first task should be passed as argument when you call the function you define for the second task.
You may need to use:
the index to access individual character (particularly the first character of each word in the sentence)
move the first character to the end of the word
add "ay" at the end of the word
Requirements:
txt = "StopAndSmellTheRose"
for chr in txt:
if chr.isupper():
txt=txt.replace(chr,' '+chr.lower())
txt=txt.lstrip()
txt=txt.capitalize()
ans=''
print(txt)
x = txt.split(" ")
for i in range (len(x)):
x[i]=x[i].replace(x[i],x[i]+x[i][0]+"ay")
for i1 in range (len(x)):
ans=ans+" "
for i2 in range (len(x[i1])):
if(i2!=0):
ans=ans+x[i1][i2]
ans=ans.lstrip()
print(ans)
output:
Explanation: