首页 > 试题广场 >

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

[编程题]字符串最后一个单词的长度
  • 热度指数:1585463 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的若干个单词组成的句子,每个单词均由大小写字母混合构成,单词间使用单个空格分隔。输出最后一个单词的长度。

输入描述:
\hspace{15pt}在一行上输入若干个字符串,每个字符串代表一个单词,组成给定的句子。
\hspace{15pt}除此之外,保证每个单词非空,由大小写字母混合构成,且总字符长度不超过 10^3


输出描述:
\hspace{15pt}在一行上输出一个整数,代表最后一个单词的长度。
示例1

输入

HelloNowcoder

输出

13

说明

\hspace{15pt}在这个样例中,最后一个单词是 \texttt{ ,长度为 13
示例2

输入

A B C D

输出

1
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#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)
print(len(list(input().split())[-1]))
# 一行拿下兄弟们!
发表于 2025-03-27 14:30:05 回复(0)
print(len(input().split()[-1]))
仅仅这样一句对吗
发表于 2025-03-12 09:44:16 回复(0)


s=input().split()
length=[len(i) for i in s]
if len(s)==1:
    print(length[0])
else:
    print(length[len(s)-1])
输入列表化
单独计算列表中每一元素的长度
通过if-else区分是否为单个字符串


发表于 2025-03-11 14:14:45 回复(1)
import sys
for line in sys.stdin:
    a = line.split()
    print(a[-1].__len__())

发表于 2025-02-18 20:18:38 回复(0)
s = input().split(' ')
print(len(s[-1]))
发表于 2025-02-14 19:18:52 回复(0)
有个疑问,“每个单词均由大小写字母混合构成”,这句要怎么理解,是只能由大小写组成还是每个单词都必须包含大小写?


发表于 2025-02-09 12:46:11 回复(1)
s = input('').strip().split(' ')
m="".join(s)
if len(s)<=1000 and m.isalpha():
    print(len(s[-1]))
发表于 2025-01-21 10:47:35 回复(0)
str=input()
temp=str.split(".")
s=float("0."+temp[1]) if s>=0.5: print(int(temp[0])+1) else: print(int(temp[0]))
发表于 2024-12-25 12:41:51 回复(0)
import sys

for line in sys.stdin:
    a = line.split()
    print(len(a[-1]))

发表于 2024-11-07 15:19:26 回复(0)
str_ = input().split()
print(len(str_[-1]))
发表于 2024-10-25 23:10:04 回复(0)
def cal_len_world (a:str):
    list1 = a.split(' ')
    print(len(list1[-1]))

cal_len_world(input())
发表于 2024-09-20 20:02:39 回复(0)
a = input()
b = a.split()
print(len(b[-1]))
发表于 2024-08-25 02:32:36 回复(0)
str=input().strip()
str2=str.split()

print(len(str2[-1]))
发表于 2024-08-19 10:49:24 回复(0)
def length_of_last_word(s): words = s.split() return len(words[-1]) s = input("请输入字符串:") print(length_of_last_word(s))
发表于 2024-08-12 10:27:35 回复(0)
两种方法
import sys

for line in sys.stdin:
    a = line.split()
    print(len(a[-1]))

while True:
    # 写代码前记得加限定条件
    try:
        in_str = input()
        if len(in_str) > 5000&nbs***bsp;len(in_str) == 0:
            raise Exception
        
        last = in_str.strip().split(" ")[-1]
        leng = len(last)
        print(leng)
        break
    except Exception:
        print("The length of str is invalid, please input it again!")


发表于 2024-07-29 17:24:57 回复(0)
import sys

for line in sys.stdin:
str1 = line.split()  #使用split函数分割
chr1 = str1[-1]  #获取最后一个字符串
count = len(chr1)  #使用len函数计算最后一个字符串的长度
print(count)
发表于 2024-06-27 00:17:25 回复(0)
import sys

str = input()
arr = str.split()
print(len(arr[-1]))
发表于 2024-06-25 13:18:04 回复(0)