Number of Segments in a String

2017/12/6 posted in  leetcode

class Solution {
public:
    int countSegments(string s) {
        int res = 0;
        for (int i = 0; i<s.length(); i++) {
            if (s[i]!=' '&& (s[i+1]==' '|| i==s.length()-1)) {
                res++;
            }
        }
        return res;
    }
};