首页 > 试题广场 >

字符串最后一个单词的长度

[编程题]字符串最后一个单词的长度
  • 热度指数:1490007 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾

输入描述:

输入一行,代表要计算的字符串,非空,长度小于5000。



输出描述:

输出一个整数,表示输入字符串最后一个单词的长度。

示例1

输入

hello nowcoder

输出

8

说明

最后一个单词为nowcoder,长度为8   
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
	string input;
	vector<string>arr;
    while(cin>>input){
    	arr.push_back(input);
	}
	cout<<arr[arr.size()-1].length()<<endl;		
	return 0;
}

编辑于 2016-08-29 14:07:27 回复(93)
my_list = list(input())
# print(my_list)
i = -1
num = 1
if len(my_list) >= 5000:
    print("超出规定长度")
elif " " not in my_list:
    print(len(my_list))
else:
    while True:
        i -= 1
        if my_list[i] == " ":
            break
        else:
            num += 1
    print(num)

发表于 2023-03-08 21:24:55 回复(0)
import sys
for i in sys.stdin:
    b=list(i)
    del b[-1]
    c="".join(b)
    d=c.split(" ")
    print(len(d[-1]))
发表于 2022-09-24 11:47:49 回复(0)
strings = raw_input()
str_list = strings.split(' ')
last_word = str_list[len(str_list)-1]
print(len(last_word))
发表于 2022-06-21 10:34:07 回复(0)
print(len(input().split(' ')[-1]))
发表于 2021-07-11 14:57:43 回复(0)
string = input()
x = string.split()
for i in x[::-1]:
    print(len(i))
    break
直接按空格拆分单词,再直接倒序打印最后一个单词长度后休止

发表于 2021-06-07 14:19:53 回复(0)
word=input().split()[-1]
print(len(word))
两行代码就行了。
编辑于 2021-06-02 13:12:16 回复(0)
Python笨蛋解题

a = input()
a = a[::-1] 
cnt=-1
for i in a:
    cnt+=1
    if i == ' ':
        print(cnt)
        break
    elif cnt == len(a)-1:
        print(cnt+1)

发表于 2021-06-01 21:12:34 回复(0)
word = list(input().split())

print(len(word[-1]))

发表于 2021-05-29 11:39:30 回复(0)
这题凭啥是困难级别?
发表于 2021-05-27 16:36:26 回复(0)
print(len(input().split(" ").pop()))
发表于 2021-05-27 14:50:54 回复(0)
def findLenth(str):
    lenth = len(str)
    i = -1
    res = 0
    while(str[i] != " "):
        i = i-1
        res = res + 1 
        if(-i>lenth):
            break
    return res
发表于 2021-05-25 16:22:14 回复(0)
Python:
str = input()
last = str.strip().split(" ")[-1]
print(len(last))


编辑于 2021-05-07 05:18:26 回复(0)
import sys
 
for line in sys.stdin:
    # 丢弃末尾的空字符,再用空字符分割
    s = line.strip().split()
    print(len(s[-1]))

发表于 2021-05-05 21:20:37 回复(0)
str_input = input()
n = len(str_input)
i = 0

if n == 0:
    print(0)
elif n==1:
    print(1)
else:
    while str_input[n-1]!=' ':
        i += 1
        n = n-1
    print(i)
用python从后往前便利,遇到第一个空格为止,跑不出来,有没有大佬解答一下
发表于 2021-04-23 15:50:05 回复(0)
s=list(input().split())
a=s[len(s)-1]
print(len(a))
发表于 2021-04-23 13:05:24 回复(0)

先判断有没有空格吧, 如果没有空格就直接输出长度,,有空格的话split()直接按空格拆分取最后一个的长度就可以
str1 = "XSUWHQ"  if " " in str1: print(len(str1.split()[-1])) else: print(len(str1))

发表于 2021-04-18 20:29:29 回复(0)

print(len(list(input().split()).pop()))

发表于 2021-04-17 20:13:24 回复(0)
使用split()函数,将输入的字符串分割
索引最后一个值使用len()函数求出长度
list=input().split(' ')
print(len(list[-1]))

发表于 2021-04-16 23:12:04 回复(0)