C++)
#include <iostream>
#include <string>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string str;
getline(cin, str);
int ans = 0;
bool flag = false;
for (auto ch : str) {
if (isalpha(ch)) {
flag = true;
}
// 공백임
else {
if (flag) {
ans += 1;
flag = false;
}
}
}
if (flag)
ans += 1;
cout << ans;
return 0;
}
Python)
import sys
read = sys.stdin.readline
st = read().rstrip().split()
print(len(st))
C++은 문자열 전체 검사하면서 단어 갯수 검사했고
파이썬은 그냥 공백으로 나눴다.
'백준 1 > 기초' 카테고리의 다른 글
| [백준 1157] 단어 공부 (C++/Python) (0) | 2020.12.05 |
|---|---|
| [백준 2675] 문자열 반복 (C++/Python) (0) | 2020.12.05 |
| [백준 10809] 알파벳 찾기 (C++/Python) (0) | 2020.12.05 |
| [백준 8958] OX 퀴즈 (C++/Python) (0) | 2020.12.05 |
| [백준 10818] 최소, 최대 (C++/Python) (0) | 2020.12.05 |