티스토리 뷰

주어지는 문자열을 하나하나 받아들여서 4가지 종류로 분류한다.

ASCII 코드로 직접 대문자, 소문자 범위를 지정해주는 방법이 빠르고 좋은 듯 하다.

나는 Regex를 사용해보았다. Regex때문인지 되게 느림...

그리고 다음부터는 걍 C++로 해보려고.....



        static void Main(string[] args)

        {

            int small = 0;

            int large = 0;

            int num = 0;

            int space = 0;


            Regex smallRegex = new Regex(@"[a-z]");

            Regex largeRegex = new Regex(@"[A-Z]");

            Regex numRegex = new Regex(@"[0-9]");


            string sentence;


            while((sentence = Console.ReadLine()) != null)

            {

                small = 0; large = 0; num = 0; space = 0;


                for(int x = 0; x<sentence.Length;x++)

                {

                    string chr = sentence[x].ToString();

                    if (chr == " ")

                        space++;

                    else if (smallRegex.IsMatch(chr))

                        small++;

                    else if (largeRegex.IsMatch(chr))

                        large++;

                    else if (numRegex.IsMatch(chr))

                        num++;

                }

                Console.WriteLine(small + " " + large + " " + num + " " + space);

                sentence = null;

            }

        }


아 코드 하이라이터 자꾸 코드 길어지면 맛탱이가 가네 ㅜㅜ

'■ 알고리즘 > ◻ 백준' 카테고리의 다른 글

[C#]백준 10828번 : 스택  (0) 2018.08.09
[C#]백준 10823번 : 더하기 2  (0) 2018.08.09
[C#]백준 1152번 : 단어의 개수  (0) 2018.08.07
[C#]백준 10824번 : 네 수  (0) 2018.08.07
[C#]백준 9095번 : 1, 2, 3 더하기  (0) 2018.08.07
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함