Код: Выделить всё
alex mcqueen
timothy karl logan
12abcd //in this case it should remain the same since the first character is not a letter
< /code>
Я должен заработать первую букву каждого слова, а затем вернуть модифицированную строку < /p>
Я попробовал эту функцию Python < /p>
def solve(s):
ret = ""
words = s.split(" ")
capitalized = []
for word in words:
# check if the first character in the word is an alphabet
if word[0].isalpha():
first = word[0].upper() # capitalize the first character
ret += first # add it to our string
for i in range(1, len(word)):
ret += str(word[i])
else: # if the first character is not an alphabet
for i in range(0, len(word)): # basically loop from the beginning
ret += str(word[i])
# get the number of words in the sentence for space insertion
# if the words are three then you need two spaces(N-1)
cnt = len(words)
idx = 0
y = ""
# loop through checking for uppers and insert a space
for i in range(len(ret)):
if ret[i].isupper() and idx != cnt:
if i == 0:
continue
y+= " " # add a space and then increment index
y+=ret[i]
idx+=1
elif ret[i].islower():
y += ret[i]
return y
Подробнее здесь: https://stackoverflow.com/questions/797 ... ut-sentenc