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

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

by 코코쵸마 2023. 8. 1.

문제

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

 

솔루션

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* my_string, const char* alp) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char* answer = (char*)malloc(1000 * sizeof(char));
    int j = 0;
    for(int i = 0; i < strlen(my_string); i++){
        printf("%c %c\n", my_string[i], *alp);
        if(my_string[i] == *alp)
            answer[j++] = toupper(*alp);
        else
            answer[j++] = my_string[i];
    }
    answer[j] = '\0';
    return answer;
}

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