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:
The idea behind the solution of this problem is:-
for task 1: Iterate all the characters present in the string and check if the character is in upper case add the space to the resultant string and then add the character after converting it to lowercase, and if the character is in lower case add the character to the result without changing it.
for task 2: Split the string into words on the basis of space, then iterate all the words and add all the characters of word in the resultant string except the first, then add the "ay" and an space in the end.
PSEUDO CODE :
function task1(arguement string){
initialize the solution string result by the first character of string
for character in arguement string except the first character:
if character is uppercase:
add a space, then add the character to the result string after converting it into lowercase
else:
add the character without any change in the result string
return result
}
function task2(arguement string){
initialize the solution string result by empty string
split the string into list of words on the basis of space
for word in list of words:
add all the character of word except the first character to the result string,
add the first character to the result string
add the string "ay" to the result string
add the space to the result string
return result
}
function main(){
take the string from the user
pass that string to the function task1
pass the result of function task1 to the function task2
print the result returned by the function task2
}
PYTHON CODE:
def task1(s): # FUNCTION DEFINITION FOR TASK 1
res = s[0] # INITIALIZED THE RESULT STRING res WITH THE FIRST CHARACTER OF STRING
for ch in s[1:]: # ITERATE ALL THE CHARACTERS IN STRING EXCEPT FIRST CHARACTER
if ch.isupper(): # IF THE CHARACTER IS UPPERCASE MEANS ITS THE START OF NEW WORD SO ADD A SPACE AND CHANGE THE CHARACTER TO
res += " "+ch.lower() # LOWER CASE AND ADD IN TO THE RESULTANT STRING res
else: # IF THE CHARACTER IS NOT UPPER THAT MEANS ITS INTERMEDIATE CHARACTER OF THE WORD, SO ADD IT WITHOUT CHANGING
res += ch
return res # RETURN THE RESULT STRING
def task2(s): # FUNCTION DEFINITION FOR TASK 2
words = s.split() # SPLIT THE STRING INTO WORDS ON THE BASIS OF SPACE, (split FUNCTION SPLIT THE STRING BY DEFAULT BASIS OF SPACE)
res = "" # INITIALIZE THE RESULTANT STRING res WITH EMPTY STRING
for word in words: # ITERATE ALL THE WORDS PRESENT IN WORDS AFTER SPLITTING
res+=word[1:]+word[0]+"ay"+" " # ADD THE WORD TO THE RESULTANT STRING res EXCEPT THE FIRST CHARACTER, THEN ADD THE FIRST CHARACTER, THEN ADD "ay"
# IN LAST ADD THE SPACE TO SPECIFY THAT THIS WORD IS COMPLETED
return res # RETURN THE RESULTANT STRING
string = input() # TAKE INPUT STRING FROM THE USER
task1_result = task1(string) # CALL TASK1 WITH ARGUEMENT INPUT STRING
task2_result = task2(task1_result) # CALL TASK2 WITH ARGUEMENT RESULT OF FUNCTION TASK1
print("task 1: ",task1_result) # PRINT THE RESULTS
print("task 2: ",task2_result)
CODE OUTPUT:
INPUT : StopAndSmellTheRose
OUTPUT :
task 1: Stop and smell the rose
task 2: topSay ndaay mellsay hetay oseray
NOTE : If you have any doubts regarding the solution please ask in the comment section. HAPPY LEARNING!!