Кількість слів#329

Кількість слів (Розбір)

This problem asks us to find the total number of words in the given input. While there are two different ways to solve this, we will focus on the simplest approach.

int cnt = 0;
string s;
while(cin >> s) {
    cnt++;
}
cout << cnt;

The reason we use while(cin » s) here is that cin reads the input up to the first whitespace character. By repeating this process inside a loop, it continues until the end of the text, incrementing the cnt variable for each word. Finally, we print the total count using cout.