-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringReverseReplace.cs
117 lines (90 loc) · 3.29 KB
/
StringReverseReplace.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PrimitiveTypes
{
[TestClass]
public class StringReverseReplace
{
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReverseString_StringIsEmpty_ReturnsEmptyString()
{
string value = string.Empty;
string actual = ReverseString(value);
Assert.AreEqual(string.Empty, actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReverseString_StringHasOneCharacter_ReturnsSameString()
{
string value = "a";
string actual = ReverseString(value);
Assert.AreEqual("a", actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReverseString_StringHasMoreCharacters_ReturnsReversedString()
{
string value = "abc";
string actual = ReverseString(value);
Assert.AreEqual("cba", actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReplaceInString_StringIsEmpty_ReturnsEmptyString()
{
string value = string.Empty;
string actual = ReplaceInString(value, 'i', "ai");
Assert.AreEqual(string.Empty, actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReplaceInString_SearchedCharacterIsFound_ReturnsModifiedString() {
string value = "abcabcabcabcabc";
string actual = ReplaceInString(value, 'b', "###");
Assert.AreEqual("a###ca###ca###ca###ca###c", actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReplaceInString_SearchedCharacterIsNotFound_ReturnsString() {
string value = "abcabcabcabcabc";
string actual = ReplaceInString(value, 'd', "###");
Assert.AreEqual("abcabcabcabcabc", actual);
}
[TestMethod]
[TestCategory("15_String_Reverse_And_Replace")]
public void ReplaceInString_ReplaceStringIsEmpty_ReturnsString() {
string value = "abcabcabcabcabc";
string actual = ReplaceInString(value, 'b', string.Empty);
Assert.AreEqual("abcabcabcabcabc", actual);
}
private string ReplaceInString(string value, char ch, string insertion)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
if (string.IsNullOrEmpty(insertion)) {
return value;
}
char car = value[0];
string cdr = value.Substring(1);
if (car == ch) return insertion + ReplaceInString(cdr, ch, insertion);
return car + ReplaceInString(cdr, ch, insertion);
}
private string ReverseString(string value)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
if (value.Length == 1)
{
return value;
}
char car = value[0];
string cdr = value.Substring(1);
return ReverseString(cdr) + car;
}
}
}