UVA494 - Kindergarten Counting Game
# 问题描述
多组数据,每行输入一串英文,输出这个英文中有多少个单词
# 思路
通过判断这个字符为字母并且下一个字符不为字母时单词总数加1,之前感觉本题数据存在bug,比如"3d b2b",但是发现输入的最后一句要求单词是一系列的英文字符序列,所以可以不用考虑此类数据。
# 代码
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
char s[1005];
while(gets(s))
{
int ans=0;
for(int i=0;i<strlen(s);i++)
if(isalpha(s[i])&&!isalpha(s[i+1]))
ans++;
cout<<ans<<endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08