Skip to content
Open
13 changes: 13 additions & 0 deletions StringProcessingApp/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();
}
}
}
69 changes: 69 additions & 0 deletions StringProcessingApp/Services/StringService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace StringProcessingApp.Services
{
public class StringService
{
private string originalText = "";
private string currentText = "";

// Set new text
public void SetText(string text)
{
originalText = text;
currentText = text;
}

// Get current text
public string GetText()
{
return currentText;
}

// Convert to uppercase
public void ToUpperCase()
{
currentText = currentText.ToUpper();
}

// Convert to lowercase
public void ToLowerCase()
{
currentText = currentText.ToLower();
}

// Count characters
public int CountCharacters()
{
return currentText.Length;
}

// Check if contains word
public bool ContainsWord(string word)
{
return currentText.Contains(word);
}

// Replace word
public void ReplaceWord(string oldWord, string newWord)
{
currentText = currentText.Replace(oldWord, newWord);
}

// Extract substring
public string ExtractSubstring(int startIndex, int length)
{
return currentText.Substring(startIndex, length);
}

// Trim spaces
public void TrimSpaces()
{
currentText = currentText.Trim();
}

// Reset to original
public void ResetText()
{
currentText = originalText;
}
}
}
121 changes: 121 additions & 0 deletions StringProcessingApp/View/StringView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using StringProcessingApp.Services;

namespace StringProcessingApp.Views
{
public class StringView
{
private StringService service = new StringService();
private bool isRunning = true;

public void Run()
{
while (isRunning)
{
ShowMenu();
Console.Write("Choose option: ");
string choice = Console.ReadLine();

HandleChoice(choice);
}
}

private void ShowMenu()
{
Console.WriteLine("\n====== 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");
}

private void HandleChoice(string choice)
{
switch (choice)
{
case "1":
Console.Write("Enter text: ");
string input = Console.ReadLine();
service.SetText(input);
break;

case "2":
Console.WriteLine("Current Text: " + service.GetText());
break;

case "3":
service.ToUpperCase();
Console.WriteLine("Converted to UPPERCASE.");
break;

case "4":
service.ToLowerCase();
Console.WriteLine("Converted to lowercase.");
break;

case "5":
Console.WriteLine("Character Count: " + service.CountCharacters());
break;

case "6":
Console.Write("Enter word to check: ");
string word = Console.ReadLine();
bool contains = service.ContainsWord(word);
Console.WriteLine("Contains word? " + contains);
break;

case "7":
Console.Write("Enter word to replace: ");
string oldWord = Console.ReadLine();
Console.Write("Enter new word: ");
string newWord = Console.ReadLine();
service.ReplaceWord(oldWord, newWord);
Console.WriteLine("Word replaced.");
break;

case "8":
Console.Write("Enter start index: ");
int start = int.Parse(Console.ReadLine());
Console.Write("Enter length: ");
int length = int.Parse(Console.ReadLine());

try
{
string result = service.ExtractSubstring(start, length);
Console.WriteLine("Substring: " + result);
}
catch
{
Console.WriteLine("Invalid index or length.");
}
break;

case "9":
service.TrimSpaces();
Console.WriteLine("Spaces trimmed.");
break;

case "10":
service.ResetText();
Console.WriteLine("Text reset to original.");
break;

case "11":
isRunning = false;
Console.WriteLine("Exiting program...");
break;

default:
Console.WriteLine("Invalid option.");
break;
}
}
}
}