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
13 changes: 13 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using StringProcessingApp.Views;

namespace StringProcessingApp
{
class Program
{
static void Main(string[] args)
{
StringView view = new StringView();
view.Run();
}
}
}
85 changes: 85 additions & 0 deletions StringService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;

namespace StringProcessingApp.Services
{
public class StringService
{
private string _originalText;
private string _currentText;

public StringService()
{
_originalText = "";
_currentText = "";
}


public void SetText(string text)
{
_originalText = text;
_currentText = text;
}


public string GetText()
{
return _currentText;
}


public string ToUpperCase()
{
_currentText = _currentText.ToUpper();
return _currentText;
}


public string ToLowerCase()
{
_currentText = _currentText.ToLower();
return _currentText;
}


public int CountCharacters()
{
return _currentText.Length;
}


public bool ContainsWord(string word)
{
return _currentText.Contains(word);
}


public string ReplaceWord(string oldWord, string newWord)
{
_currentText = _currentText.Replace(oldWord, newWord);
return _currentText;
}


public string ExtractSubstring(int startIndex, int length)
{
if (startIndex < 0 || startIndex >= _currentText.Length || length < 0)
return "Invalid indices";
if (startIndex + length > _currentText.Length)
length = _currentText.Length - startIndex;
return _currentText.Substring(startIndex, length);
}


public string TrimSpaces()
{
_currentText = _currentText.Trim();
return _currentText;
}


public void ResetText()
{
_currentText = _originalText;
}
}
}
140 changes: 140 additions & 0 deletions StringView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
}