SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
More About Strings
Exercise:
Programming Exercises
Question:11 | ISBN:9780132576376 | Edition: 2

Question

Word Separator
Write a program that accepts as input a sentence in which all of the words are run together but the first character of each word is uppercase. Convert the sentence 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 “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.”

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Word Separator Program Code:

def convertSentence(inputData):
    # Initialize an empty string to store the converted sentence
    convertedString = ""

    # Iterate through each character in the input sentence
    for i in range(len(inputData)):
        # Check if the current character is uppercase
        if inputData[i].isupper():
            # If it's not the first character, add a space before the uppercase character
            if i > 0:
                convertedString += " "
            # Convert the uppercase character to lowercase (except for the first character)
            if i > 0:
                convertedString += inputData[i].lower()
            else:
                convertedString += inputData[i]
        else:
            # If the character is not uppercase, add it to the converted sentence as is
            convertedString += inputData[i]

    # Return the final converted sentence
    return convertedString


# Test the function with the given example
inputData = "StopAndSmellTheRoses."
outputData = convertSentence(inputData)
print(outputData)

Executed Output:

Stop and smell the roses.

 

0 0

Discussions

Post the discussion to improve the above solution.