[BOJ/1264번] 모음의 개수

sky·2022년 8월 22일
0

BaekJoon Online Judge(B)

목록 보기
81/98
post-thumbnail

문제

Bronze Ⅳ

영문 문장을 입력받아 모음의 개수를 세는 프로그램을 작성하시오. 모음은 'a', 'e', 'i', 'o', 'u'이며 대문자 또는 소문자이다.

입력
입력은 여러 개의 테스트 케이스로 이루어져 있으며, 각 줄마다 영어 대소문자, ',', '.', '!', '?', 공백으로 이루어진 문장이 주어진다. 각 줄은 최대 255글자로 이루어져 있다.

입력의 끝에는 한 줄에 '#' 한 글자만이 주어진다.

출력
각 줄마다 모음의 개수를 세서 출력한다.


Solution

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++과 비슷하게 풀었다.


Total Time

  • 2022-08-22 | 21:35 - 22:35 Success!

Review
이게 뭐라고 1시간을 썼을까..

profile
개발자가 되고 싶은 1人

0개의 댓글

관련 채용 정보