以下是使用 Python 实现的代码示例: ```python def count_words(text): text = text.replace(',', ' ') text = text.replace('.', ' ') text = text.replace('!', ' ') words = text.split() return len(words) text = input("请输入一行字符:") print(count_words(text)) ``` 这段代码首先将输入字符串中的逗号、句号、感叹号替换为空格,然后使用`split`方法以空格为分隔符将字符串拆分成单词列表,最后返回单词列表的长度,即为单词的个数。 [2024-11-10 21:59:31 | AI写代码神器 | 131点数解答]