从数组c ++打印正面和背面

Print front and back from an array c++

how can i print from array once from back and once from front c++?

例如:

char c[] = { 'A','B','C','D' };
for (int i = 0; i < size(arr); i++)
{
    cout << c[i]; 
}

输出将是ABCD

但我想要的输出应该是 ADBC

从前面打印一个,从后面打印一个 c[0],c[3],c[1],c[2]

可以使用。这里我们只迭代数组大小的一半。请注意,这假设您在数组中有偶数个元素。

int main()
{
    char c[] = { 'A','B','C','D','E' };
    std::size_t len = std::size(c);
    //---------------------v------------->divide by 2 so that we iterate only half the size times
   for (int i = 0; i < len/2; i++)
   {
        std::cout << c[i]<<c[len - i - 1];
   }
   if (len % 2) {//in case we have odd number of elements 
        std::cout << c[len/2];
    }
    
}

Working demo