[백준/BOJ] 10808번_알파벳 개수 (C++/Java)

JIMIN·2023년 1월 26일

BOJ_Bronze

목록 보기
20/75

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

문제


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


입력


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


출력


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



💻 예제 입력

baekjoon

💻 예제 출력

1 1 0 0 1 0 0 0 0 1 1 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0

C++ 소스코드


#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string str;
    cin >> str;
    
    for (int i = 'a'; i < 'z'+1; i++)
    {
        int count = 0;
        for (int j = 0; j < str.size(); j++)
        {
            if (str[j] == i) { count++; }
        }
        cout << count << " ";
    }
    cout << "\n";
}

Java 소스코드


import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        sc.close();
        
        for (int i = 'a'; i < 'z'+1; i++) {
            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(j) == i) count++;
            }
            System.out.print(count + " ");
        }
    }
}
profile
잘못된 코드나 정보가 있다면 알려주세요! 👋🏻

0개의 댓글