단어 세기
영어 단어들이 공백으로 나뉜 한 줄이 주어진다.
공백은 여러 개 이어질 수도 있고 줄의 앞뒤에 있을 수도 있다.
단어가 모두 몇 개인지 출력하라.
- 입력
- 길이 1 ~ 200, 단어는 1개 이상
- 출력
- 단어 수
hello big world
3
영어 단어들이 공백으로 나뉜 한 줄이 주어진다.
공백은 여러 개 이어질 수도 있고 줄의 앞뒤에 있을 수도 있다.
단어가 모두 몇 개인지 출력하라.
hello big world
3
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
공백이 여러 개면 나누는 방법에 따라 답이 달라요: Python split()(인자 없음)은 알아서 묶어 주지만 split(" ")는 빈 조각까지 세요.
Java는 trim() 뒤 split("\\s+"), C#은 RemoveEmptyEntries.
C·C++은 scanf("%s")·cin >>가 공백을 건너뛰어서 읽은 횟수가 곧 답이에요.
print(len(input().split()))const line = require("fs").readFileSync(0, "utf8").split("\n")[0];console.log(line.split(" ").filter((w) => w !== "").length);import { readFileSync } from "node:fs";const line: string = readFileSync(0, "utf8").split("\n")[0];console.log(line.split(" ").filter((w) => w !== "").length);import java.util.Scanner; public class Main { public static void main(String[] args) { String line = new Scanner(System.in).nextLine().trim(); System.out.println(line.split("\\s+").length); }}using System; class Program{ static void Main() { string line = Console.ReadLine(); Console.WriteLine(line.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length); }}#include <iostream>#include <string> int main() { std::string w; int count = 0; while (std::cin >> w) count++; std::cout << count << '\n';}#include <stdio.h> int main(void) { char w[201]; int count = 0; while (scanf("%200s", w) == 1) count++; printf("%d\n", count); return 0;}line = 콘솔.줄읽기();words = line.나누기("");콘솔.쓰기(words.개수);