int countWords(string str) { stringstream s(str); string word; int count = 0; while (s >> word) count++; return count; }