C++ using StringStream


  • 0
    T

    Basically just counting the number of strings that are space separated within a longer string.

    int countSegments(string s)
    {
        stringstream ss(s);
        char delim = ' ';
        int count = 0;
        
        string word;
        
        while (getline(ss, word, delim)) {
            if (!word.empty()) {
                count++;
            }
        }
        return count;
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.