본문 바로가기

Algorithm/문제풀이

[백준] 1152번: 단어의 개수

문제링크 https://www.acmicpc.net/problem/1152


문제풀이

입력한 문장의 모든 띄어쓰기 개수를 센 다음, 문장의 맨앞, 맨 뒤에 공백이 오는 경우를 고려해 카운트 다운한다.


소스코드

/*
 * 백준 1152번: 단어의 개수
 */

#include <iostream>
#include <string>

int main() {
	std::string str;
	getline(std::cin, str);

	int words = 0;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] == ' ') words++;
	}

	if (str[0] == ' ') words--;
	if (str[str.length() - 1] == ' ') words--;

	std::cout << ++words;

	return 0;
}


'Algorithm > 문제풀이' 카테고리의 다른 글

[백준] 1193번: 분수찾기  (0) 2019.03.22
[백준] 2292번: 벌집  (0) 2019.03.22
[백준] 2438번: 별 찍기 - 1  (0) 2019.03.18
[백준] 2839번: 설탕 배달  (0) 2019.03.18
[백준] 1000번: A+B  (0) 2019.03.13