Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using StringProcessingApp.Views;

// Program entry point
StringView view = new StringView();
view.Run();
37 changes: 37 additions & 0 deletions StringService.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
84 changes: 84 additions & 0 deletions StringView.cs
Original file line number Diff line number Diff line change
@@ -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: ");
}
}
}