牛客网 - 在线编程 - 华为机试 - 字符串最后一个单词的长度

题目描述

计算字符串最后一个单词的长度,单词以空格隔开。 

输入描述:

一行字符串,非空,长度小于5000。

输出描述:

整数N,最后一个单词的长度。

示例1

输入

hello world

输出

5

C++:

#include <iostream>
using namespace std;

int main()
{
    string str;
    while (cin >> str);
    cout << str.size() << endl;
    return 0;    
}

Python:

print(len(input().split(' ')[-1]))

猜你喜欢

转载自blog.csdn.net/qq_39735236/article/details/81564707