-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convertstringtocamelcase.cs
48 lines (41 loc) · 1.67 KB
/
Convertstringtocamelcase.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
using System;
// Convert string to camel case ( 6 Kyu )
// Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).
// Examples
// "the-stealth-warrior" gets converted to "theStealthWarrior"
// "The_Stealth_Warrior" gets converted to "TheStealthWarrior"
// ALGORITHMS, REGULAR EXPRESSIONS, DECLARATIVE PROGRAMMING, ADVANCED LANGUAGE FEATURES, FUNDAMENTALS, STRINGS
namespace CodeWars
{
public class Kata
{
public static string ToCamelCase(string str)
{
string[] split = str.Split('-', '_');
var charsToRemove = new string[] { "-", "_" };
for (int i = 1; i < split.Length; i++)
{
string word = split[i];
int wordLength = word.Length;
string eachWordFirstLetter = word.Substring(0, 1).ToUpper();
string rest = word.Substring(1, wordLength - 1).ToLower();
string result = eachWordFirstLetter + rest;
str = str.Replace(word, result);
}
foreach (var c in charsToRemove)
{
str = str.Replace(c, string.Empty);
}
return str;
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Kata.ToCamelCase("the-stealth-warrior"));
Console.WriteLine(Kata.ToCamelCase("The_Stealth_Warrior"));
Console.WriteLine(Kata.ToCamelCase("david_Garcia-saragih"));
}
}
}