-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitStrings.cs
50 lines (40 loc) · 1.5 KB
/
SplitStrings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
namespace CodeWars
{
// Split Strings ( 6 Kyu )
// Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').
// Examples:
// SplitString.Solution("abc"); // should return ["ab", "c_"]
// SplitString.Solution("abcdef"); // should return ["ab", "cd", "ef"]
// ALGORITHMS, REGULAR EXPRESSIONS, DECLARATIVE PROGRAMMING, ADVANCED LANGUAGE FEATURES, FUNDAMENTALS, STRINGS
public class SplitString
{
public static string[] Solution(string str)
{
if (str.Length % 2 == 1)
{
str += "_";
}
List<string> list = new List<string>();
for (int i = 0; i < str.Length; i += 2)
{
list.Add(str.Substring(i, 1) + str.Substring(i+1, 1));
}
return list.ToArray();
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(SplitString.Solution("abc"));
Console.WriteLine(SplitString.Solution("abcd"));
Console.WriteLine(SplitString.Solution("abcdef"));
Console.WriteLine(SplitString.Solution("ab"));
Console.WriteLine(SplitString.Solution("abcdefgh"));
Console.WriteLine(SplitString.Solution("abcdefg"));
Console.ReadLine();
}
}
}