Bronze Ⅳ
영문 문장을 입력받아 모음의 개수를 세는 프로그램을 작성하시오. 모음은 'a', 'e', 'i', 'o', 'u'이며 대문자 또는 소문자이다.
입력
입력은 여러 개의 테스트 케이스로 이루어져 있으며, 각 줄마다 영어 대소문자, ',', '.', '!', '?', 공백으로 이루어진 문장이 주어진다. 각 줄은 최대 255글자로 이루어져 있다.
입력의 끝에는 한 줄에 '#' 한 글자만이 주어진다.
출력
각 줄마다 모음의 개수를 세서 출력한다.
C++
#include <iostream>
using namespace std;
int main() {
string s, vowel[] = {"a", "e", "i", "o", "u"};
int cnt = 0;
while(s != "#"){
getline(cin, s);
for(int i=0; i < s.length(); i++){
for(int j=0; j<5; j++){
if(s[i] == vowel[j])
cnt++;
}
}
cout << cnt << '\n';
}
return 0;
}
에러가 난다.. 아래 코드가 정답이다.
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
while(1){
int cnt = 0;
getline(cin, s);
for(int i=0; i<s.length(); i++){
s[i] = tolower(s[i]); //전체 소문자로 변환
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
}
if(s == "#") break;
cout << cnt << '\n';
}
return 0;
}
Python
while 1:
n = input()
if n == '#': break;
cnt = 0
n = n.lower()
for char in n:
if char in ('a', 'e', 'i', 'o', 'u'):
cnt = cnt + 1
print(cnt)
c++과 비슷하게 풀었다.
Review
이게 뭐라고 1시간을 썼을까..