Question

In: Computer Science

Write a program to perform the following two tasks: 1. The program will accept a string...

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:

  • Algorithm must be modularized
  • Pseudocode in .txt file or .docx file
  • Python program (upload your .py file)

Solutions

Expert Solution

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!!


Related Solutions

Write a program to perform the following two tasks: 1. The program will accept a string...
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...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name and program title on the output screen. 2. Display instructions for the user. 3. Prompt the user to enter two numbers. 4. Calculate the sum, difference, product, (integer) quotient and remainder of the numbers. 5. Display a terminating message. Requirements: 1. The main procedure must be divided into sections:  introduction  get the data  calculate the required values  display the results...
Program Description Write and test a MASM program to perform the following tasks: Display your name...
Program Description Write and test a MASM program to perform the following tasks: Display your name and program title on the output screen. Display instructions for the user. Prompt the user to enter three numbers (A, B, C) in descending order. Calculate and display the sum and differences: (A+B, A-B, A+C, A-C, B+C, B-C, A+B+C). Display a closing message. Program Requirements The program must be fully documented and laid out according to the CS271 Style Guide. This includes a complete...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT