I would start off by analysing what is going on. As I see it, there are a number of fixed-length lines. The length of each line is equal to the total number of lines. The number of lines needs to be an odd number and >1 so that you have a middle line to give the diamond effect. Initially there is a central ‘*’ character and then a new * is added to the left and right of the existing stars as the line number increases. This carries on until the middle line has been printed, then the process is reversed and the most recently added * to the left and the right is then replaced by a space.
Although a string is char
array, it is read only. You can, however, use a char[]
, manipulate that and then convert it to a string before printing it. Something like
var currentLine = new string(' ', lines);
char[] characters = currentLine.ToCharArray();
You can then add ‘*’ to the
characters
array by indexing into it. You will need a
leftposition
and
rightposition
variable along with an
increment
variable. The increment is set to -1 after the middle line is printed.
characters[lpos] = charToPaste;
characters[rpos] = charToPaste;
Console.WriteLine(new string(characters));
lpos -= increment;
rpos += increment;
You need only a single loop to do this.