본문 바로가기
프로그래머스코딩테스트연습풀이/C++

[프로그래머스/C++] 특정한 문자를 대문자로 바꾸기

by 코코쵸마 2025. 2. 14.

문제

영소문자로 이루어진 문자열 my_string과 영소문자 1글자로 이루어진 문자열 alp가 매개변수로 주어질 때, my_string에서 alp에 해당하는 모든 글자를 대문자로 바꾼 문자열을 return 하는 solution 함수를 작성해 주세요.

 

솔루션

#include <string>
#include <vector>
#include <cctype>

using namespace std;

string solution(string my_string, string alp) {
    string answer = "";
    for(int i = 0; i < my_string.size(); i++){
        if(my_string[i] == alp[0]){
            answer += toupper(alp[0]);
        }
        else{
            answer += my_string[i];
        }
    }
    return answer;
}

 

https://school.programmers.co.kr/learn/courses/30/lessons/181873

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr