[BaekJoon] 1264번 - 모음의 개수 [Java][C++]
[BaekJoon] 1264번 - 모음의 개수 [Java][C++]
1. 문제 풀이
주어진 영문 문장에서 모음의 개수를 세는 문제로 대소문자가 섞여 있어서 모음인지 비교할 각 문자를 전부 소문자로 변환해서 판단해줬다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line.equals("#")) break;
int cnt = 0;
for (int i = 0; i < line.length(); i++) {
char c = Character.toLowerCase(line.charAt(i));
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') cnt++;
}
sb.append(cnt).append("\n");
}
System.out.println(sb);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (true) {
string line;
getline(cin, line);
if (line == "#") break;
int cnt = 0;
for (int i = 0; i < line.size(); i++) {
char c = tolower(line[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') cnt++;
}
cout << cnt << '\n';
}
}
This post is licensed under CC BY 4.0 by the author.