본문 바로가기
코테/프로그래머스

문자 반복 출력하기

by Slow Motion~ 2023. 2. 22.
728x90

처음에 이렇게 풀었더니 아스키코드 값 연산이 되버림

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, int n) {
    string answer = "";
    for(int i = 0; i <= my_string.size()-1; i++){
        answer += my_string[i]*n; 
    }
    return answer;
}

그래서

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, int n) {
    string answer = "";
    for(int i = 0; i <= my_string.size()-1; i++){
        for(int j = 0; j<n; j++){
        answer += my_string[i]; }
    }
    return answer;
}

 

'코테 > 프로그래머스' 카테고리의 다른 글

문자열 정렬하기(1) C++  (0) 2023.03.21
특정 문자 제거  (0) 2023.02.22
문자열 뒤집기(for문에 대해)  (0) 2023.02.22
각도기  (0) 2023.02.21
배열 뒤집기  (0) 2023.02.21

댓글