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

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

namespace StringService
{
public class StringService
{
private string currentText;

public StringService()
{
currentText = string.Empty;
}

public string CurrentText => currentText;

public void SetText(string text)
{
currentText = text;
}

public string ConvertToUpper()
{
return currentText.ToUpper();
}

public string ConvertToLower()
{
return currentText.ToLower();
}

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

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

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

public string ExtractSubstring(int startIndex, int length)
{
if (startIndex < 0 || startIndex + length > currentText.Length)
{
return "Invalid substring range!";
}

return currentText.Substring(startIndex, length);
}

public string TrimSpaces()
{
return currentText.Trim();
}

public void ResetText()
{
currentText = string.Empty;
}
}
}
182 changes: 182 additions & 0 deletions Views/StringViews
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using StringService;

namespace StringViews
{
public class StringView
{
private StringService stringService;

public StringView()
{
stringService = new StringService();
}

public void ShowMenu()
{
while (true)
{
Console.Clear();
Console.WriteLine("1. Enter text.");
Console.WriteLine("2. View Current text.");
Console.WriteLine("3. Convert to upper case.");
Console.WriteLine("4. Convert to lower case.");
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.Write("Choose an option: ");

string choice = Console.ReadLine();

switch (choice)
{
case "1":
EnterText();
break;
case "2":
ViewCurrentText();
break;
case "3":
ConvertToUpper();
break;
case "4":
ConvertToLower();
break;
case "5":
CountCharacters();
break;
case "6":
CheckIfContainsWord();
break;
case "7":
ReplaceWord();
break;
case "8":
ExtractSubstring();
break;
case "9":
TrimSpaces();
break;
case "10":
ResetText();
break;
case "11":
Exit();
return;
default:
Console.WriteLine("Invalid option, please try again!");
Console.ReadKey();
break;
}
}
}

private void EnterText()
{
Console.Write("Enter text: ");
string text = Console.ReadLine();
stringService.SetText(text);
Console.WriteLine("Text has been set.");
Console.ReadKey();
}

private void ViewCurrentText()
{
Console.WriteLine("Current text: " + stringService.CurrentText);
Console.ReadKey();
}

private void ConvertToUpper()
{
Console.WriteLine("Text in upper case: " + stringService.ConvertToUpper());
Console.ReadKey();
}

private void ConvertToLower()
{
Console.WriteLine("Text in lower case: " + stringService.ConvertToLower());
Console.ReadKey();
}

private void CountCharacters()
{
Console.WriteLine("Character Count: " + stringService.CountCharacters());
Console.ReadKey();
}

private void CheckIfContainsWord()
{
Console.Write("Enter word to check: ");
string word = Console.ReadLine();

if (stringService.ContainsWord(word))
{
Console.WriteLine($"The text contains the word '{word}'.");
}
else
{
Console.WriteLine($"The text does not contain the word '{word}'.");
}

Console.ReadKey();
}

private void ReplaceWord()
{
Console.Write("Enter word to replace: ");
string oldWord = Console.ReadLine();

Console.Write("Enter new word: ");
string newWord = Console.ReadLine();

Console.WriteLine("Updated Text: " + stringService.ReplaceWord(oldWord, newWord));
Console.ReadKey();
}

private void ExtractSubstring()
{
Console.Write("Enter starting index: ");

if (!int.TryParse(Console.ReadLine(), out int startIndex))
{
Console.WriteLine("Invalid number!");
Console.ReadKey();
return;
}

Console.Write("Enter length of substring: ");

if (!int.TryParse(Console.ReadLine(), out int length))
{
Console.WriteLine("Invalid number!");
Console.ReadKey();
return;
}

Console.WriteLine("Substring: " + stringService.ExtractSubstring(startIndex, length));
Console.ReadKey();
}

private void TrimSpaces()
{
Console.WriteLine("Text after trim: " + stringService.TrimSpaces());
Console.ReadKey();
}

private void ResetText()
{
stringService.ResetText();
Console.WriteLine("Text has been reset.");
Console.ReadKey();
}

private void Exit()
{
Console.WriteLine("Exiting... Thank you!");
}
}
}