개발자가 되

프로그래머스 Lv.0

Lv.0 문자열 섞기

dltjdud 2025. 1. 19. 20:50

문제설명

길이가 같은 두 문자열 str1과 str2가 주어집니다.
두 문자열의 각 문자가 앞에서부터 서로 번갈아가면서 한 번씩 등장하는 문자열을 만들어 return 하는 solution 함수를 완성해 주세요.

 

제한사항

  • 1 ≤ str1의 길이 = str2의 길이 ≤ 10
    • str1과 str2는 알파벳 소문자로 이루어진 문자열입니다.

 

입출력 

str1 str2 result
"aaaaa" "bbbbb" "ababababab"

 

풀이

 

JAVA

class Solution {
    public String solution(String str1, String str2) {
        String answer = "";

        for(int i = 0; i < str1.length(); i++){
            answer+= str1.charAt(i);
            answer+= str2.charAt(i);
        }

        return answer;
    }
}

 

Python

def solution(str1, str2):
    answer = ''
    for i in range(len(str1)):
        answer+=str1[i]+str2[i]
    return answer

 

C

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* str1, const char* str2) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int str1len = strlen(str1);

    char* answer = (char*)malloc(str1len * 2 + 1);

    int idx = 0;

   for(int i = 0; i < str1len; i++)
   {
       answer[idx++] = str1[i];
       answer[idx++] = str2[i];
   }

    answer[idx] = '\0';


    return answer;
}

'프로그래머스 Lv.0' 카테고리의 다른 글

Lv.0 문자열 곱하기  (0) 2025.01.20
Lv.0 문자 리스트를 문자열로 변환하기  (0) 2025.01.19
Lv.0 문자열 겹쳐쓰기  (0) 2025.01.18
Lv.0 홀짝 구분하기  (0) 2025.01.18
Lv.0 문자열 돌리기  (0) 2025.01.14