Question: How is it possible to print the following pattern?

```
    *
   **
  ***
 ****
*****
****
***
**
*

```

How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:

```
#include <iostream>

using namespace std;

void printpattern(int n)
{
    for (int r = 0; r <= n; r++)
    {
        for (int z = 0; z <= r; z++) {
            cout << "*";
        }

        cout << endl;
    }
}

int main()
{
    int n = 5;
    printpattern(n);
}

```

Answer: 
Although comments suggest using `std::string`, I believe this was intended to be written using only loops. This works:

```
void printpattern(int n)
{
  // print first half.
  for (int i = 0; i < n; ++i) {
    // print spaces
    for (int r = n - i; r > 0; --r)
      std::cout << ' ';

    // print stars.
    for (int j = i; j > 0; --j) 
      std::cout << '*';

    std::cout << '\n';
  }

  /// print second half. No need to print spaces here.
  for (int i = 1; i <= n; ++i) {
    for (int r = n - i; r >= 0; --r) 
      std::cout << '*';

    std::cout << '\n';
  }
}

```
It's easier to do this by composing a longer string and using a sliding view.

That's pretty straightforward in C++:

```
#include <iostream>
#include <string>
#include <string_view>

void printpattern(std::size_t n)
{
    const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
    for (std::size_t i = 1;  i < n*2;  ++i)
        std::cout << std::string_view(s.data()+i, n) << '\n';
}

int main()
{
    printpattern(5);
}

```

You could of course make the space padding be of length `n-1` on both sides, and use a more conventional loop starting i at zero:

```
    const auto s = std::string(n - 1, ' ') + std::string(n, '*')
                 + std::string(n-1, ' ');
    for (std::size_t i = 0;  i < n * 2 - 1;  ++i)

```

It's up to you whether saving two characters of temporary string is worthwhile.