본문 바로가기

BOJ

[백준] 1152번 단어의 개수 c++

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	ios::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);
	int count = 0;
	int count2 = 1;
	string str;
	getline(cin, str);
	if (str[0] == ' ') {
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == ' ') count++;
		}
		if (str[str.length() - 1] == ' ') {
			cout << --count;
			return 0;
		}
		cout << count;
	}
	else {
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == ' ') count2++;
		}
		if (str[str.length() - 1] == ' ') {
			cout << --count2;
			return 0;
		}
		cout << count2;
	}
	return 0;
}

'BOJ' 카테고리의 다른 글

[백준] 5622번 다이얼 c++  (0) 2020.04.26
[백준] 2908번 상수 c++  (0) 2020.04.26
[백준] 1157번 단어공부 c++  (0) 2020.04.26
[백준] 8958번 OX퀴즈 c++  (0) 2020.04.26
[백준] 10996번 별 찍기 -21 c++  (0) 2020.04.26