Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections.Generic;
- namespace Assignment
- {
- public struct BoldText
- {
- public int indexFirst;
- public int indexLast;
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- List<BoldText> bold = new List<BoldText>();
- Console.WriteLine("Type the text You want to change");
- string text = Console.ReadLine();
- int inputEnd = 1;
- while(inputEnd != 0)
- {
- Console.WriteLine("Declare the beginning of the bolded text");
- int first = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Declare the ending of the bolded text");
- int last = Convert.ToInt32(Console.ReadLine());
- bold.Add( new BoldText {indexFirst = first, indexLast = last} );
- Console.WriteLine("If You want to stop declaring, type 0, otherwise type any other number");
- inputEnd = Convert.ToInt32(Console.ReadLine());
- }
- changeToHtml(text, bold);
- }
- public static void changeToHtml(string text, List<BoldText> bold)
- {
- int textLength = text.Length;
- bool[] isCharTagged = new bool[textLength];
- foreach(BoldText bText in bold)
- {
- if((bText.indexFirst < textLength) && (bText.indexFirst+bText.indexLast < textLength)) // Verification if indexes do not exceed the length.
- {
- var changedText = new StringBuilder(); // A new string, where the new text will be written.
- int letter = 0; // Sort of a index of the text (excluding the HTML tag).
- char prev = ' '; // A variable for the "previous" character.
- foreach (char curr in text)
- {
- if((letter == 0) && (letter == bText.indexFirst))
- {
- changedText.Append("<b>"); // The HTML tag for the begging of the bold text is added.
- if (letter == (bText.indexFirst+bText.indexLast))
- {
- changedText.Append("</b>"); // The HTML tag for the ending of the bold text is added.
- }
- }
- changedText.Append(curr); // Adding the character to the string.
- if((!isTag(prev, curr)) && (!isCharTagged[bText.indexFirst]) && (!isCharTagged[bText.indexFirst+bText.indexLast]))
- {
- if (++letter == bText.indexFirst) // If the number of the letter equals the first index.
- {
- changedText.Append("<b>"); // The HTML tag for the begging of the bold text is added.
- }
- if (letter == (bText.indexFirst+bText.indexLast)) // If the number of the letter equals the last index.
- {
- changedText.Append("</b>"); // The HTML tag for the ending of the bold text is added.
- }
- }
- prev = curr; // Saving the character (it becomes the "previous" one).
- }
- for (int j = bText.indexFirst; j < (bText.indexFirst+bText.indexLast); j++) // The text between the indexes is tagged.
- {
- isCharTagged[j] = true;
- }
- text = changedText.ToString();
- }
- }
- Console.WriteLine(text);
- }
- public static bool isTag(char prev, char curr)
- {
- if ((curr == '<') || (curr == '/') || ((curr == 'b') && ((prev == '<') || (prev == '/'))) || (curr == '>'))
- return true;
- else
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement