Day18. Number of Segments in a String(434)

问题描述:
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.

Example

Input: "Hello, my name is John"
Output: 5
/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
    var count = 0; 
    var arr = s.split(' ');
    for(var i in arr){
        if(arr[i] !== ""){
            count++;
        }
    }
     return count;
};

文末彩蛋
欧盟,美国,中国,俄罗斯,日本


Day18. Number of Segments in a String(434)_第1张图片
卫星

你可能感兴趣的:(Day18. Number of Segments in a String(434))