[백준] C# : 단어의 개수(1152번)

ssu_hyun·2022년 8월 24일
0

Data Structure & Algorithm

목록 보기
58/67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine() + ' ';
            int count = 0;

            // 반례 : 모두 공백으로 입력할 경우 문자열이 없음에도 무조건 1을 출력
            for (int i = 0; i < input.Length - 1; i++)
            {
                if (input[i] != ' ' && input[i + 1] == ' ')
                {
                    count += 1;
                }
                else
                {
                    count += 0;
                    continue;
                }
            }
            if (count == 0)
            {
                Console.WriteLine(0);
            }
            else
            {
                Console.WriteLine(count);
            }
        }
    }
}

0개의 댓글