Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program, in a file called playWithStrings.py, which reads a number of sets of input, each set containing a word followed by a sentence

Write a program, in a file called playWithStrings.py, which reads a number of sets of input, each set containing a word followed by a sentence (you may assume this is a series of words, where each word is separated by a single space, ended by a period), and includes and tests the following functions to process each of the given sets:

  • function encodeTheWord which takes a word (i.e., the given word from the set) and encodes and returns it as follows: the first character of the word is followed by the last character of the word which is followed by the second character and then the second last character of the word and so on. For example, given the word that, the function should return ttha. If the given word has an odd number of characters, then the middle character is repeated again. For example, given the word mouse it should be encoded as meosuu
  • function findAndReplace which, given the word and the sentence, finds each occurrence of the given word in the sentence and replaces it with the encoded word (as returned by the encodeTheWord function). The function prints the original sentence and the encoded sentence.
  • function main which tests all the functions and prints the required data with appropriate labels as shown in the output below.

Note: The given input words should be at least 4 characters long; if they are not, a message should be printed and the next word should be input. You may use the sentinel "ZZZ" as the last word to indicate the end of the input. You are not permitted to use any of the built-in string manipulation functions, except for the len() function to find the length of the word and/or sentence.

For example, given the following as input:

that that that and that is that. abc oops oops I made a mistake in the last word oops. there there there now we are almost there now. ZZZ 

The output should be as follows (input shown in blue):

Enter word (ZZZ to exit): that Enter sentence: that that and that is that. The original given word is: that The encoded word is: ttha The original sentence is: that that and that is that. The coded sentence is: ttha ttha and ttha is ttha. Enter word (ZZZ to exit): abc Word must be at least 4 characters long. Enter word (ZZZ to exit): oops Enter sentence: oops I made a mistake in the last word oops. The original given word is: oops The encoded word is: osop The original sentence is: oops I made a mistake in the last word oops. The coded sentence is: osop I made a mistake in the last word osop. Enter word (ZZZ to exit): there Enter sentence: there there now we are almost there now. The original given word is: there The encoded word is: tehree The original sentence is: there there now we are almost there now. The coded sentence is: tehree tehree now we are almost tehree now. Enter word (ZZZ to exit): ZZZ

image text in transcribed

uestion 3 Write a program, in a file called playwithstrings.py, which reads a number of sets of input, each set containing a word followed by a sentence (you may assume this is a series of words, where each word is separated by a single space, ended by a period), and includes and tests the following functions to process each of the given sets function encodeThelord which takes a word (i.e., the given word from the set) and encodes and returns it as follows: the first character of the word is followed by the last character of the word which is followed by the second character and then the second last character of the word and so on. For example, given the word that, the function should return ttha. If the given word has an odd number of characters, then the middle character is repeated again. For example, given the word mouse it should be encoded as meosuu . function findAndReplace which, given the word and the sentence, finds each occurrence of the given word in the sentence and replaces it with the encoded word (as returned by the encode Theword function). The function prints the original sentence and the encoded sentence . function main which tests all the functions and prints the required data with appropriate labels as shown in the output below Note: The given input words should be at least 4 characters long; if they are not, a message should be printed and the next word should be input. You may use the sentinel "ZZZ" as the last word to indicate the end of the input. You are not permitted to use any of the built-in string manipulation functions, except for the len) function to find the length of the word and/or sentence. For example, given the following as input: that that that and that is that abc oops oops I made a mistake in the last word oops. there there there now we are almost there now zzZ The output should be as follows (input shown in blue) Enter word (ZZZ to exit): that Enter sentence: that that and that is that The original given word is: that The encoded word is: ttha The original sentence is: that that and that is that The coded sentence is: ttha ttha and ttha is ttha Enter word (ZZZ to exit): abc Word must be at least 4 characters long Enter word (ZZZ to exit): oops Enter sentence: oops I made a mistake in the last word oops The original given word is: oops The encoded word is: osop The original sentence is: oops I made a mistake in the last word oops. The coded sentence is: osop I made a mistake in the last word osop Enter word (ZZZ to exit): there Enter sentence: there there now we are almost there now. The original given word is: there The encoded word is: tehree The original sentence is: there there now we are almost there now The coded sentence is: tehree tehree now we are almost tehree now Enter word (ZZZ to exit): ZzZ

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions