【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a011
#include <iostream>
using namespace std;
int main() {
string s;
bool isNew;
int cnt;
while (getline(cin, s)) {
cnt=0;
isNew = false;
for (int i=0; i<s.length(); i++) {
if (isalpha(s[i])) isNew = true;
else {
if (isNew) {
cnt++;
isNew = false;
}
}
}
cout << cnt + isNew << endl;
}
return 0;
}
Python code (credit: Amy Chou)
while True:
try:
S = input()
if len(S) == 0:
print(0)
break
count = 0
newWord = 1
for i in range(len(S)):
if S[i].isalpha():
newWord = 0
else:
if not newWord:
count += 1
newWord = 1
count += (not newWord)
print(count)
except:
break