PTA-第三章-7-14 统计一行文本的单词个数 (15分)

本题目要求编写程序统计一行字符中单词的个数。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。

输入格式:
输入给出一行字符。

输出格式:
在一行中输出单词个数。

输入样例:

Let's go to room 209.

输出样例:

5

代码

s=input()
count=0
if s[-1].isspace():
    for i in range(len(s)-1):
        if (not s[i].isspace() )  and s[i+1].isspace():
            count+=1
else:
    for i in range(len(s)-1):
        if ( not s[i].isspace() ) and s[i+1].isspace():
            count+=1
    count+=1
print(count)

发布了24 篇原创文章 · 获赞 2 · 访问量 200

猜你喜欢

转载自blog.csdn.net/weixin_45115928/article/details/104440627