Код: Выделить всё
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(" ")
for word in words:
# check if the first letter 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 letter is not an alphabet
for i in range(0, len(word)): # basically loop from the beginning
ret += str(word[i])
cnt = len(words) # this is the number of words in the sentence
idx = 0
y = ""
for i in range(len(ret)):
if i != 0 and ret[i].isupper() and idx != cnt:
y+= " " # add a space and then the letter
y+= ret[i]
idx +=1 # increment the counter
else:
# handle the exceptions here including a mid word starting with a number
return y
Подробнее здесь: https://stackoverflow.com/questions/797 ... ut-sentenc