알고리즘/BaekJoon
[c++] 1152번 : 단어의 개수
__jhp_+
2022. 3. 10. 14:32
https://www.acmicpc.net/problem/1152
1152번: 단어의 개수
첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 공백 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한 문자열
www.acmicpc.net
#include <iostream>
#include <string>
#define TRIM_SIGN " \t\n\r\v\f"
inline std::string& lTrim(std::string& str, const char* drop = TRIM_SIGN)
{
str.erase(0, str.find_first_not_of(drop));
return str;
}
inline std::string& rTrim(std::string& str, const char* drop = TRIM_SIGN)
{
str.erase(str.find_last_not_of(drop) + 1);
return str;
}
inline std::string& Trim(std::string& str, const char* drop = TRIM_SIGN)
{
rTrim(lTrim(str));
return str;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::string str = "";
int nCount = 0;
std::getline(std::cin, str);
Trim(str, " ");
if (str.length() > 0)
{
++nCount;
}
for (int i = 0; i < str.length(); ++i)
{
if (str[i] == ' ')
{
++nCount;
}
}
std::cout << nCount << '\n';
return 0;
}