175k views
0 votes
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."

User Delmy
by
4.3k points

1 Answer

0 votes

Answer:

Following are the code to this question:

value=input("Enter string value: ")#defining string variable for input value from user

val= ""#defining another string variable val

i=0#defining integer variable and assign value 0

for x in value:#defining loop for proving spacing in value

if x.isupper() and i > 0:#defining if block to check value upper and greatern then 0

val+= " "#proving spacing in string value

val += x.lower()#convert another value into lower case

else:#defining else block

val += x#hold value in val variable

i += 1#increment i variable value

print (val)#print value

Output:

Enter string value: StopAndSmellTheRoses

Stop and smell the roses

Step-by-step explanation:

In the above python code, a string variable "value" is defined that inputs the value from the user end, and another variable "val" is defined for saving calculated value.

In the next step, for loop is declared, that uses if block to check input value and convert the input value first letter into uppercase and print all value with space. If the condition is false it will return the user input value.

User Optional
by
5.4k points