一、计算字符串最后一个单词的长度
倒序遍历法:
def lengthlastword(s):
count = 0
for i in s[::-1]:
if i == ' ' and count != 0:
return count
elif i != ' ':
count += 1
return count
s = input()
print(lengthlastword(s))
一、计算字符串最后一个单词的长度
倒序遍历法:
def lengthlastword(s):
count = 0
for i in s[::-1]:
if i == ' ' and count != 0:
return count
elif i != ' ':
count += 1
return count
s = input()
print(lengthlastword(s))