Algorithm/BOJ

[BOJ] 10808 알파벳 개수

Game Client Lee Hwanguk 2023. 1. 20. 23:47

https://www.acmicpc.net/problem/10808

 

10808번: 알파벳 개수

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

www.acmicpc.net

문제

알파벳 소문자로만 이루어진 단어 S가 주어진다. 각 알파벳이 단어에 몇 개가 포함되어 있는지 구하는 프로그램을 작성하시오.

입력

첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다.

출력

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //[BOJ] 10808 알파뱃 갯수


            string input=Console.ReadLine(); //string형식의 입력값
            List<int> inputNum = new List<int>(); //input 값을 int로 바꿔야한다
            for (int i=0; i<input.Length; i++)
            {
                inputNum.Add(Convert.ToInt32(input[i])); //input을 int형식으로 바꾼 후 inputNum에 넣는다 
            }

            List<int> askiList = new List<int>(); //aski코드를 담을 List 생성(int)형식
            for(int i=0; i<25; i++) //해석하려는 알파벳이 26글자(0~25)
            {
                askiList.Add(0); //askiList를 모두 0으로 초기화 한 후 비교하면서 1로 변경해보자
            }

            for(int i=0; i<inputNum.Count; i++) //입력받은 inputNum의 count만 큼 순회하며
            {
                for(int j=0; j <=askiList.Count; j++)
                {
                    if (inputNum[i] == j+97) //aski코드의 a~z는 97~125
                    {
                        askiList[j]++; //0으로 초기화되어있는 askiList를 1씩 증가
                    }
                }
            }
            foreach(int output in askiList) //출력해서 확인
            {
                Console.Write("{0}",output);
            }
        }




        
    }
}

#string 형식을 int형식의 List에 담으면 아스키 코드가된다

#비어있는 int형식의 List를 만들고

#for문으로 비교하며 맞다면 1씩 증가하면

#askicode로 변경된 값이 출력이된다