AOAPC I: Beginning Algorithm Contests (Rujia Liu) ::Volume 0. Getting Started
Description
给一个字符串,计算其中有多少个单词。
其中对单词的定义是,一段连续的由小写或大写字母组成的序列。
Type
Water
Analysis
声明一个布尔值bo,记录前一个字符是否为字母。
然后遍历这个字符串,如果当前字符为字母,且bo为false的话,
说明这是单词中的第一个字母,记录下来即可。
Solution
// UVaOJ 494
// Kindergarten Counting Game
// by A Code Rabbit
#include <cstdio>
#include <cstring>
char str[10000];
int cnt;
bool bo;
int main() {
while (gets(str)) {
cnt = 0;
bo = false;
for (int i = 0; i < strlen(str); i++) {
if ('a' <= str[i] && str[i] <= 'z' ||
'A' <= str[i] && str[i] <= 'Z')
{
if (!bo) cnt++;
bo = true;
} else {
bo = false;
}
}
printf("%d\n", cnt);
}
return 0;
}