以下是使用 C++解决此问题的代码: ```cpp #include <iostream> #include <string> int countWords(std::string str) { int count = 0; bool inWord = false; for (char c : str) { if (c =='') { if (inWord) { count++; inWord = false; } } else { inWord = true; } } if (inWord) { count++; } return count; } int main() { std::string input; std::getline(std::cin, input); int wordCount = countWords(input); std::cout << wordCount << std::endl; return 0; } // 额外注意逻辑点: // 1. 注意输入字符串可能为空的情况。 // 2. 考虑输入字符串中可能存在的特殊字符处理。 // 3. 对于字符串的遍历边界情况要仔细处理。 ``` [2025-03-23 20:52:59 | AI写代码神器 | 243点数解答]