Ejercicio
Texto a HTML
Objetivo
Crear una clase "TextToHTML", que debe ser capaz de convertir varios textos introducidos por el usuario en una secuencia HTML, como esta:
Hola
Soy yo
Ya he terminado
debería convertirse en
Hola
Soy yo
Ya he terminado
La clase debe contener:
Una matriz de cadenas
Un método "Add", para incluir una nueva cadena en él
Un método "Display", para mostrar su contenido en pantalla
Un método "ToString", para devolver una cadena que contiene todos los textos, separados por "n".
Cree también una clase auxiliar que contenga una función "Main", para ayudarle a probarla.
Código de Ejemplo
// Importing the System namespace to handle console functionalities and string manipulations
using System;
using System.Collections.Generic;
public class TextToHTML
{
// An array to hold the strings entered by the user
private List textList;
// Constructor initializes the list to store strings
public TextToHTML()
{
textList = new List(); // Using List instead of array for dynamic resizing
}
// Method to add a new string to the list
public void Add(string newText)
{
textList.Add(newText);
}
// Method to display all the strings in the list, each on a new line
public void Display()
{
// Loop through each string in the list and print it
foreach (var text in textList)
{
Console.WriteLine(text);
}
}
// Method to return the strings as a single string with newline separators
public override string ToString()
{
// Join all strings with "\n" and return the result
return string.Join("\n", textList);
}
}
// Auxiliary class with the Main function to test the TextToHTML class
public class Program
{
public static void Main()
{
// Create a new instance of TextToHTML
TextToHTML textToHTML = new TextToHTML();
// Adding some texts
textToHTML.Add("Hola");
textToHTML.Add("Soy yo");
textToHTML.Add("Ya he terminado");
// Displaying the contents of the TextToHTML object
Console.WriteLine("Display Method Output:");
textToHTML.Display();
// Displaying the ToString output
Console.WriteLine("\nToString Method Output:");
Console.WriteLine(textToHTML.ToString());
}
}