-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringView.cs
More file actions
140 lines (127 loc) · 4.68 KB
/
StringView.cs
File metadata and controls
140 lines (127 loc) · 4.68 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using StringProcessingApp.Services;
namespace StringProcessingApp.Views
{
public class StringView
{
private StringService _service;
public StringView()
{
_service = new StringService();
}
public void Run()
{
bool exit = false;
while (!exit)
{
DisplayMenu();
Console.Write("Choose an option: ");
string input = Console.ReadLine();
Console.WriteLine();
switch (input)
{
case "1":
EnterText();
break;
case "2":
ViewCurrentText();
break;
case "3":
Console.WriteLine("Uppercase: " + _service.ToUpperCase());
break;
case "4":
Console.WriteLine("Lowercase: " + _service.ToLowerCase());
break;
case "5":
Console.WriteLine("Character count: " + _service.CountCharacters());
break;
case "6":
CheckContainsWord();
break;
case "7":
ReplaceWord();
break;
case "8":
ExtractSubstring();
break;
case "9":
Console.WriteLine("Trimmed text: " + _service.TrimSpaces());
break;
case "10":
_service.ResetText();
Console.WriteLine("Text reset to original.");
break;
case "11":
exit = true;
Console.WriteLine("Exiting...");
break;
default:
Console.WriteLine("Invalid option. Try again.");
break;
}
Console.WriteLine();
}
}
private void DisplayMenu()
{
Console.WriteLine("=== String Processing System ===");
Console.WriteLine("1. Enter Text");
Console.WriteLine("2. View Current Text");
Console.WriteLine("3. Convert to UPPERCASE");
Console.WriteLine("4. Convert to lowercase");
Console.WriteLine("5. Count Characters");
Console.WriteLine("6. Check if Contains Word");
Console.WriteLine("7. Replace Word");
Console.WriteLine("8. Extract Substring");
Console.WriteLine("9. Trim Spaces");
Console.WriteLine("10. Reset Text");
Console.WriteLine("11. Exit");
Console.WriteLine("================================");
}
private void EnterText()
{
Console.Write("Enter your text: ");
string text = Console.ReadLine();
_service.SetText(text);
Console.WriteLine("Text saved successfully.");
}
private void ViewCurrentText()
{
string text = _service.GetText();
Console.WriteLine("Current Text: " + (string.IsNullOrEmpty(text) ? "[Empty]" : text));
}
private void CheckContainsWord()
{
Console.Write("Enter word to check: ");
string word = Console.ReadLine();
bool exists = _service.ContainsWord(word);
Console.WriteLine(exists ? $"Text contains '{word}'." : $"Text does not contain '{word}'.");
}
private void ReplaceWord()
{
Console.Write("Enter word to replace: ");
string oldWord = Console.ReadLine();
Console.Write("Enter new word: ");
string newWord = Console.ReadLine();
string result = _service.ReplaceWord(oldWord, newWord);
Console.WriteLine("Updated Text: " + result);
}
private void ExtractSubstring()
{
Console.Write("Enter start index: ");
if (!int.TryParse(Console.ReadLine(), out int start))
{
Console.WriteLine("Invalid input.");
return;
}
Console.Write("Enter length: ");
if (!int.TryParse(Console.ReadLine(), out int length))
{
Console.WriteLine("Invalid input.");
return;
}
string substring = _service.ExtractSubstring(start, length);
Console.WriteLine("Substring: " + substring);
}
}
}