180k views
5 votes
4. Word Separator:Write a program that accepts as input a sentence in which all of thewords 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.”

*python coding

User Jmgomez
by
7.7k points

1 Answer

3 votes

sent = input("")

count = 0

new_sent = ""

for i in sent:

if count == 0:

new_sent += i

count += 1

else:

if i.isupper():

new_sent += " "+i.lower()

else:

new_sent += i

print(new_sent)

The above code works if the user enters the sentence.

def func(sentence):

count = 0

new_sent = ""

for i in sentence:

if count == 0:

new_sent += i

count += 1

else:

if i.isupper():

new_sent += " " + i.lower()

else:

new_sent += i

return new_sent

print(func("StopAndSmellTheRoses."))

The above code works if the sentence is entered via a function.

User Amarie
by
8.0k points