작성일 :

문제 링크

15184번 - Letter Count

설명

문장에서 알파벳만 세어 대소문자를 구분하지 않고 빈도 그래프를 출력하는 문제입니다.


접근법

한 줄을 읽고 알파벳만 세어 26개의 카운트를 만들고, A부터 Z까지 순서대로 막대를 출력합니다.
각 줄은 A | 뒤에 등장 횟수만큼 *를 붙이는 형식입니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Text;

class Program {
  static void Main() {
    var line = Console.ReadLine()!;
    var cnt = new int[26];

    foreach (var ch in line) {
      if ('A' <= ch && ch <= 'Z') cnt[ch - 'A']++;
      else if ('a' <= ch && ch <= 'z') cnt[ch - 'a']++;
    }

    var sb = new StringBuilder();
    for (var i = 0; i < 26; i++) {
      sb.Append((char)('A' + i)).Append(" | ");
      if (cnt[i] > 0) sb.Append(new string('*', cnt[i]));
      sb.AppendLine();
    }

    Console.Write(sb);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string line; getline(cin, line);
  int cnt[26] = {0, };

  for (char ch : line) {
    if ('A' <= ch && ch <= 'Z') cnt[ch - 'A']++;
    else if ('a' <= ch && ch <= 'z') cnt[ch - 'a']++;
  }

  for (int i = 0; i < 26; i++) {
    cout << char('A' + i) << " | ";
    if (cnt[i] > 0) cout << string(cnt[i], '*');
    cout << "\n";
  }

  return 0;
}