본문 바로가기
  • Let's study
PS/백준

[C++] 백준 5358: Football Team

by 코딩고수이고파 2023. 5. 15.

문제

https://www.acmicpc.net/problem/5358

 

5358번: Football Team

Print the same list of names with every ‘i’ replaced with an ‘e’, every ‘e’ replaced with an ‘i’, every ‘I’ replaced with an ‘E’, and every ‘E’ replaced with an ‘I’.

www.acmicpc.net

 

해결방법

while문 안에 getline(cin, str) 함수를 사용하여 입력값이 없을 때 까지 입력받고 문자열을 for문으로 하나씩 보면서 e는 i로, E는 I로, i는 e로, I는 E로 변경 후 출력한다.

 

코드

#include<iostream>
#include<string>

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	string str;

	while (getline(cin, str)) {
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == 'e')
				str[i] = 'i';
			else if (str[i] == 'E')
				str[i] = 'I';
			else if (str[i] == 'i')
				str[i] = 'e';
			else if (str[i] == 'I')
				str[i] = 'E';
		}

		cout << str << '\n';
	}

	return 0;
}

댓글