I'm not going to give you the code, but I'll give you an idea of the algorithm.
First, you need to convert from letter to number, which I'll call n
, so A
is 0, B
is 1, and so on. I put A as 0 and not 1 because I think it will be easier. This is important in order to be able to count how many lines and how many spaces are needed.
To do this, you use the ASCII table, where A
is the number 65. Therefore, to do this conversion, simply subtract 65 from the chosen character.
Then we have to draw the diamond, line by line. Let's split the task in two, as we have the upper half and the lower half of the diamond. In the upper half, we have% w and% rows, so using a loop n
iterating from 0 to for
is the best idea.
For each row, we also have two halves (and therefore two other n
loops). The left half has% w and% characters, and all of these characters, with the exception of only one of them, will be whitespace. To find the exception, assuming you're using a variable called for
, you'd use n
. The character to be printed is the opposite of the conversion to number made from j
, so just use n-j
on it.
For the right half of the line, you iterate j
times back-to-front what you did in the left half.
Made the top half of the diamond, to make the bottom half you use a strategy similar to the right half of each line, repeating what is in the top half loop, but iterating j+65
times back-to-front.