개선 코드
* Linq
using System;
using System.Linq;
public class Solution {
public string solution(string my_string, int n) {
return string.Concat(my_string.Select(ch => new string(ch, n)));
}
}
.Select : 문자열의 각 문자(ch)를 변환
ch => new string(ch, n) : 각 문자를 n번 반복한 문자열로 변환
.Concat : 변환된 문자열을 하나로 합침

=> 문자열의 각 문자를 -> 그 문자를 n번 반복한 문자열로 바꿔서 -> 모두 합쳐줘
cf. 다른 LINQ 방법
return new string(my_string.SelectMany(ch => Enumerable.Repeat(ch, n)).ToArray());
* new string(char, int) 생성자 사용
using System;
// 입력 : my_string 문자열, 정수 n(반복할 수)
// 출력 : my_string을 n만큼 반복한 문자열
public class Solution {
public string solution(string my_string, int n) {
string answer = "";
foreach (char ch in my_string)
{
answer += new string(ch, n); // char를 n번 반복한 문자열 생성
}
return answer;
}
}
* new string() 생성자 가능한 오버로드들 (4개가 전부)
-> char 배열로 생성(1), char 반복(3) !

* 스트링빌더 사용
using System;
using System.Text;
// 입력 : my_string 문자열, 정수 n(반복할 수)
// 출력 : my_string을 n만큼 반복한 문자열
public class Solution {
public string solution(string my_string, int n) {
// StringBuilder
StringBuilder sb = new StringBuilder();
foreach (char ch in my_string)
{
for (int i = 0; i < n; i++)
{
sb.Append(ch);
}
}
return sb.ToString();
}
}
8/11 다시 푼 것
using System;
// 입력 : my_string 문자열, 정수 n(반복할 수)
// 출력 : my_string을 n만큼 반복한 문자열
public class Solution {
public string solution(string my_string, int n) {
string answer = "";
// 1. my_string 개수 만큼 반복
foreach (char ch in my_string)
//for (int i = 0; i < my_string.Length; i++)
{
// 2. n만큼 반복
// 문제 1. int -> string (x)
for(int i = 0; i < n; i++)
{
//Console.Write(ch);
// +로 문자열에 추가 연산
answer =+ ch;
//answer =+ my_string[i].ToString();
//Console.Write(my_string[i]);
}
}
return answer;
}
}

말이 안 되는 오류인데...... !!!!!!
-> 원인 : =+ !
answer =+ ch는,
answer = +ch로 해석되어 ch의 양수 값을 answer에 대입하는 것...
1. 문자열 : foreach문 사용해서 char랑 인덱스로 사용 가능
2. 문자열에 문자를 추가하려면 + 사용 가능
3. 대입 연산자 : =+ 가 아니라, += !!!!!!!!!!!!!!!!!!
=+ : 는 플러스,,,
(=+ : 대입 후 양수, += : 더해서 대입)
8/8 다시 푼 것
using System;
using System.Collections.Generic;
// 입력 : 문자열 my_string, 정수 n
// 출력 : 문자열 answer
// my_string에 들어있는 각 문자를 n만큼 반복
public class Solution {
public string solution(string my_string, int n) {
//string answer = "";
// ? : string에는 append나, add가 됨?
List<char> charList = new List<char>();
charList = my_string.ToCharArray();
List<char> answerList = new List<char>();
// 반복문 : n까지
// 문제 2. 각 문자열이 아니라, 각 문자(char)를 반복해야함
/*
for (int i = 0; i < n; i++)
{
charList.Add(my_string[i]);
}
*/
// charList가 아니라, my_string!
foreach (char ch in charList)
{
for (int i = 0; i < n; i++)
{
// 문제 3 : i가 아니라, char를 추가!
//answerList.Add(i);
answerList.Add(ch);
}
}
//return answer;
//return charList.ToString();
// 문제 : 실행한 결괏값 "System.Collections.Generic.List`1[System.Char]"이 기댓값 "hhheeellllllooo"과 다릅니다.
// ? : char to string
// -> new string에 넣기
string answer = new string(answerList);
return answer;
}
}
8/7 풀던 것
using System;
using System.Collections.Generic;
// 입력 : 문자열 my_string, 정수 n
// 출력 : 문자열 answer
// my_string에 들어있는 각 문자를 n만큼 반복
public class Solution {
public string solution(string my_string, int n) {
string answer = "";
// 문자열에 추가? <- string에 바로 추가가 되는지 ?
// 리스트 선언해서, 추가하고 string으로 변환해야할듯 ?!
// 문제 1. string말고, char!
//List<string> answerList = new List<string>();
List<char> answerList = new List<char>();
/*
for(int i = 0; i < n; i++)
{
answer + my_string[i];
}
*/
// 이중 반복문?
foreach (char str in my_string)
{
//answer + str;
//Console.Write(answer);
answerList.Add(str);
//Console.Write(str);
Console.Write(answerList);
}
return answer;
}
}'C# > 복습 완료' 카테고리의 다른 글
| 문자열 정렬하기 (2) (복습 완료) (0) | 2025.09.02 |
|---|---|
| 주사위의 개수 (복습 완료) (0) | 2025.08.21 |
| 직각삼각형 출력하기 (복습 완료) (0) | 2025.08.14 |
| [C#]숨어있는 숫자의 덧셈(1) - 복습 완료 (0) | 2025.08.07 |