问题描述:
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;
};