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.”
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.