Given a string s, return the number of segments in the string.

A segment is defined to be a contiguous sequence of non-space characters.

&nbsp;
Example 1:

Input: s = "Hello, my name is John"
Output: 5
Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]


Example 2:

Input: s = "Hello"
Output: 1


&nbsp;
Constraints:


	0 &lt;= s.length &lt;= 300
	s consists of lowercase and uppercase English letters, digits, or one of the following characters "!@#$%^&amp;*()_+-=',.:".
	The only space character in s is ' '.


class Solution:
    def countSegments(self, s: str) -> int:
        return len(s.split())
class Solution {
    public int countSegments(String s) {
        int length = 0;
        boolean flag = false;

        for(Character c : s.toCharArray()) {
            if(c == ' ' && flag) {
                length++;
                flag = !flag;
            } else if(c != ' ') {
                flag = true;
            }
        }

        return flag ? length + 1 : length;
    }
}
class Solution {
public:
    int countSegments(string s) {
        if(s=="")
            return 0;
        int res=0,flag=0;
        for(int i=0;i<size(s);i++){
            if(s[i]!=' '){
                i++;
                while(i<size(s) and s[i]!=' '){
                    i++;
                }
                res++;
            }
        }
        return res;
    }
};
/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
    return s.trim() ? s.trim().split(/\s+/).length : 0
};
Number of Segments in a String