topic
Description
Calculate the length of the last word of a string, words are separated by spaces, and the length of the string is less than 5000. (Note: The end of the string does not end with a space)
Input description:
Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.
Output description:
Output an integer representing the length of the last word of the input string.
Example 1
输入:hello nowcoder
输出:8
说明:最后一个单词为nowcoder,长度为8
the code
const rl = require("readline").createInterface({
input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
while ((line = await readline())) {
console.log(findLastWord(line));
}
})();
function findLastWord(line) {
return line.slice(line.lastIndexOf(" ") + 1).length;
}