diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..a96a505 --- /dev/null +++ b/Program.cs @@ -0,0 +1,5 @@ +using StringProcessingApp.Views; + +// Program entry point +StringView view = new StringView(); +view.Run(); diff --git a/StringService.cs b/StringService.cs new file mode 100644 index 0000000..9b254e6 --- /dev/null +++ b/StringService.cs @@ -0,0 +1,37 @@ +namespace StringProcessingApp.Services +{ + public class StringService + { + private string _originalText = ""; + private string _currentText = ""; + + public void SetText(string text) + { + _originalText = text; + _currentText = text; + } + + public string GetCurrentText() => _currentText; + + public void ToUpper() => _currentText = _currentText.ToUpper(); + + public void ToLower() => _currentText = _currentText.ToLower(); + + public int GetLength() => _currentText.Length; + + public bool ContainsWord(string word) => _currentText.Contains(word); + + public void ReplaceWord(string oldWord, string newWord) + => _currentText = _currentText.Replace(oldWord, newWord); + + public void ExtractSubstring(int startIndex, int length) + { + if (startIndex >= 0 && startIndex + length <= _currentText.Length) + _currentText = _currentText.Substring(startIndex, length); + } + + public void TrimSpaces() => _currentText = _currentText.Trim(); + + public void ResetText() => _currentText = _originalText; + } +} diff --git a/StringView.cs b/StringView.cs new file mode 100644 index 0000000..8366459 --- /dev/null +++ b/StringView.cs @@ -0,0 +1,84 @@ +using StringProcessingApp.Services; + +namespace StringProcessingApp.Views +{ + public class StringView + { + private readonly StringService _service = new StringService(); + + public void Run() + { + bool running = true; + while (running) + { + DisplayMenu(); + string choice = Console.ReadLine() ?? ""; + + switch (choice) + { + case "1": + Console.Write("Enter new text: "); + _service.SetText(Console.ReadLine() ?? ""); + break; + case "2": + Console.WriteLine($"Current Text: [{_service.GetCurrentText()}]"); + break; + case "3": + _service.ToUpper(); + break; + case "4": + _service.ToLower(); + break; + case "5": + Console.WriteLine($"Character Count: {_service.GetLength()}"); + break; + case "6": + Console.Write("Word to find: "); + string word = Console.ReadLine() ?? ""; + Console.WriteLine(_service.ContainsWord(word) ? "Found!" : "Not found."); + break; + case "7": + Console.Write("Word to replace: "); + string oldW = Console.ReadLine() ?? ""; + Console.Write("New word: "); + string newW = Console.ReadLine() ?? ""; + _service.ReplaceWord(oldW, newW); + break; + case "8": + Console.Write("Start index: "); + int start = int.Parse(Console.ReadLine() ?? "0"); + Console.Write("Length: "); + int len = int.Parse(Console.ReadLine() ?? "0"); + _service.ExtractSubstring(start, len); + break; + case "9": + _service.TrimSpaces(); + break; + case "10": + _service.ResetText(); + Console.WriteLine("Text reset to original."); + break; + case "11": + running = false; + break; + default: + Console.WriteLine("Invalid option."); + break; + } + Console.WriteLine("\nPress any key to continue..."); + Console.ReadKey(); + } + } + + private void DisplayMenu() + { + Console.Clear(); + Console.WriteLine("--- String Processing System ---"); + Console.WriteLine("1. Enter Text\n2. View Current Text\n3. Convert to UPPERCASE"); + Console.WriteLine("4. Convert to lowercase\n5. Count Characters\n6. Check if Contains Word"); + Console.WriteLine("7. Replace Word\n8. Extract Substring\n9. Trim Spaces"); + Console.WriteLine("10. Reset Text\n11. Exit"); + Console.Write("\nSelect an option: "); + } + } +}