Trying to learn coding in my 30s.
Could anybody review my code and give me some feedback if possible? It works, but I am sure there are better ways to solve this problem.
The problem:
A text is given. Write a program that modifies the casing of letters to uppercase at all places in the text surrounded by <upcase>
and </upcase>
tags. Tags cannot be nested.
Here is what I've come up with:
namespace Task6Casing
{
class Program
{
static void Main(string[] args)
{
Console.Write("Text: ");
string text = Console.ReadLine();
char[] textNew = new char[text.Length];
int i = 0, j = 0, k = 0;
while (i < text.Length)
{
if (text.IndexOf("<upcase>", k) < 0) // check if there is any <upcase> tag
{ // if no, copy everyting
while (i < text.Length)
{
textNew[j++] = text[i++];
}
}
else
{
while (i < text.IndexOf("<upcase>", k)) // if there is an <upcase> tag, copy letters until the tag
{
textNew[j++] = text[i++];
}
i += 8; // move index i to the position right next to the <upcase> tag
k = i;
}
if (text.IndexOf("</upcase>", k) < 0) // check if there is any </upcase> tag
{ // if no, copy everyting in CAPITAL letters
while (i < text.Length)
{
textNew[j++] = Char.ToUpper(text[i++]);
}
}
else
{
while (i < text.IndexOf("</upcase>", k)) // if there is an </upcase> tag, copy letters in CAP letters until the tag
{
textNew[j++] = Char.ToUpper(text[i++]);
}
i += 9; // move index i to the position right next to the </upcase> tag
}
}
foreach(char c in textNew)
{
Console.Write(c);
}
Console.WriteLine();
}
}
}