Little Jay

[C++] 백준 1152번 단어의 개수 본문

알고리즘/BOJ

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

Jay, Lee 2021. 3. 21. 11:30
#include <iostream>
#include <string>
using namespace std;
int main() {

    string s;
    getline(cin, s);
    int count = 0;

    if (s.empty())
    {
        cout << "0";
        return 0;
    }
    count = 1;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == ' ')
            count++;
    }
    if (s[0] == ' ')
        count--;
    if (s[s.length() - 1] == ' ')
        count--;

    cout << count;
    return 0;
}

 

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 11050번 이항 계수1  (0) 2021.03.21
[C++] 백준 10773번 제로  (0) 2021.03.21
[C++] 백준 10809번 알파벳 찾기  (0) 2021.03.15
[C++] 백준 2577번 숫자의 개수  (0) 2021.03.14
[C++] 백준 10814번 나이순 정렬  (0) 2021.03.11
Comments