URI Online Judge Solution 1234 Dancing Sentence Using Python Programming Language.
A sentence is called dancing if its first letter is uppercase and the case of each subsequent letter is the opposite of the previous letter. Spaces should be ignored when determining the case of a letter. For example, "A b Cd" is a dancing sentence because the first letter ('A') is uppercase, the next letter ('b') is lowercase, the next letter ('C') is uppercase, and the next letter ('d') is lowercase.
Input
The input contains several test cases. Each test case is composed by one line that contais a string sentence. This string will contain between 1 and 50 characters ('A'-'Z','a'-'z' or space ' '), inclusive, or at least, one letter ('A'-'Z','a'-'z').
Output
Turn the sentence into a dancing sentence (like following examples) by changing the cases of the letters where necessary. All spaces in the original sentence must be preserved, ie, " sentence " must be converted in " SeNtEnCe ".
Solution Using Python:
while True:
try:
Str1 = ""
Str2 = input()
FirstChar = True
for l in Str2:
if l == ' ':
Str1 += ' '
continue
if FirstChar:
Str1 += l.upper()
FirstChar = False
else:
Str1 += l.lower()
FirstChar = True
print(Str1)
except EOFError:
break
Comments
Post a Comment