[Codewars #59] Double Char (8kyu)

[Codewars #59] Double Char (8kyu) 문제 풀이

Posted by karais89 on January 28, 2019

Instructions

링크

Given a string, you have to return a string in which each character (case-sensitive) is repeated once.

1
2
3
4
5
DoubleChar("String") == "SSttrriinngg"

DoubleChar("Hello World") == "HHeelllloo WWoorrlldd"

DoubleChar("1234!_ ") == "11223344!!__ "

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Text;

public class Kata
{
  public static string DoubleChar(string s)
  {
    // your code here
    StringBuilder doubleStr = new StringBuilder();
    for (int i = 0; i < s.Length; i++)
    {
      doubleStr.Append(s[i]);
      doubleStr.Append(s[i]);
    }
    return doubleStr.ToString();
  }
}
  • 각 문자 2번씩 반복 하기.

Best Practices

1
2
3
4
5
6
7
8
9
10
using System;
using System.Linq;

public class Kata
{
  public static string DoubleChar(string s)
  {
    return string.Join("", s.Select(x => "" + x + x));
  }
}
  • Linq 사용.