在一行上输入若干个字符串,每个字符串代表一个单词,组成给定的句子。除此之外,保证每个单词非空,由大小写字母混合构成,且总字符长度不超过 。
在一行上输出一个整数,代表最后一个单词的长度。
HelloNowcoder
13
在这个样例中,最后一个单词是 ,长度为 。
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; }
string = input() x = string.split() for i in x[::-1]: print(len(i)) break
word=input().split()[-1] print(len(word))
word = list(input().split()) print(len(word[-1]))
str = input() last = str.strip().split(" ")[-1] print(len(last))
import sys for line in sys.stdin: # 丢弃末尾的空字符,再用空字符分割 s = line.strip().split() print(len(s[-1]))
str1 = "XSUWHQ" if " " in str1: print(len(str1.split()[-1])) else: print(len(str1))
print(len(list(input().split()).pop()))
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题