문제 링크: https://www.acmicpc.net/problem/1152
백준 문자열 6단계 - 1152번 단어의 개수를 풀어보았다.
풀이: 문자열 한 줄을 입력받고 공백으로 띄어진 단어의 수를 센다.
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string inp;
getline(cin,inp);
int cnt = 1;
if(inp.empty()) cout << "0";
else
{
for(int i = 0; i < inp.length(); i++)
{
if(inp[i] == ' ') cnt++;
}
if(inp[0] == ' ') cnt--;
if(inp[inp.length()-1] == ' ') cnt--;
cout << cnt;
}
}
Python
arr=list(input().split())
print(len(arr))
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
다이얼 풀이 (0) | 2023.01.02 |
---|---|
상수 풀이 (0) | 2023.01.02 |
단어 공부 풀이 (0) | 2022.12.31 |
문자열 반복 풀이 (0) | 2022.12.31 |
알파벳 찾기 풀이 (0) | 2022.12.31 |