C++/Codeforces

C++[118A] String Task 문제해석 및 풀이

S_Hoon 2020. 8. 4. 06:36

http://codeforces.com/problemset/problem/118/A

 

Problem - 118A - Codeforces

 

codeforces.com

Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it:

  • deletes all the vowels,
  • inserts a character "." before each consonant,
  • replaces all uppercase consonants with corresponding lowercase ones.

Vowels are letters "A", "O", "Y", "E", "U", "I", and the rest are consonants. The program's input is exactly one string, it should return the output as a single string, resulting after the program's processing the initial string.

Help Petya cope with this easy task.

 

Petya는 프로그래밍 레슨을 참가하기 시작했습니다. 첫 번째 수업에서 그의 업무는 간단한 프로그램을 짜는 것이었습니다.

그 프로그램의 설명은 다음과 같습니다. 주어진 String이 라틴 대소문자로 이루어져 있다면, 

  • 모든 모음을 지우기,
  • 각각의 자음 앞에 "."을 추가,
  • 대문자인 자음들을 같은 글자의 소문자로 변경.

모음들은 "A", "O", "Y", "E", "U", "I", 그리고 나머지는 전부 자음입니다. 프로그램의 인풋은 정확하게 하나의 String이어야 합니다. 

반대로도 마찬가지로 하나의 String으로 출력해야 합니다. 

Petya가 이 업무를 잘 처리할 수 있도록 도와주세요.

 

Input

The first line represents input string of Petya's program. This string only consists of uppercase and lowercase Latin letters and its length is from 1 to 100, inclusive.

Output

Print the resulting string. It is guaranteed that this string is not empty.

 

인풋

첫 번째 줄은 Petya의 프로그램의 input String을 의미합니다. 이 String은 라틴 대, 소문자로만 이루어져있고 철자수 리밋은 1부터 100까지입니다.

아웃풋

결과 String을 출력하세요, 이 String은 비어있지 않습니다.


문제풀이

이 문제도 상당히 간단합니다.

String 하나를 입력한 후 

1. 모두 소문자로 바꾼다

2. 모든 모음들을 없앤다

3. 반복을 이용해 자음을 출력하는 동시에 "."을 추가한다

 

#include <iostream>
#include <string> // string을 사용할 수 있게 해줌

using namespace std;

int main(){
    string s;
    cin >> s;
    
    // 반복문을 이용해서 String을 하나하나 접근할 수 있도록 함
    for(int i = 0; i < s.length(); i++){
        char result = tolower(s[i]); // 변수를 선언한 이유는 같은 "tolower(s[i])"를 계속쓰면 귀찮기 때문
        // 모음을 만나면 아무것도 안하고 다음 반복으로 넘어감, 
        // 예를 들어, i = 2인데 모음을 만난다면 아무것도 안하고 바로 i = 3으로 넘어감
        if(result=='a' || result=='e' || result=='i' || result=='o' || result=='u' || result=='y') continue;
	// 자음이라면 모든 철자마다 "."을 출력하고 그 다음에 자음을 하나씩 출력, 
        // endl; 을 사용하지 않았기 때문에 한 줄에 출력됨
		else cout << "." << result;
    }
    return 0;
}

매번 문제를 만나면

처음에는 훨씬 쉬운 방법들을 뒤로두고 쓸데없이 삽질을 많이합니다.

그래도 코딩테스트는 노력의 영역이니 처음에 잘 안된다고 포기하면 곤란합니다.

 

'C++ > Codeforces' 카테고리의 다른 글

C++[1A] Theatre Square 문제해석 및 풀이  (0) 2020.08.04
C++ [59A] Word 문제해석 및 풀이  (0) 2020.08.04