How to calculate reactance of a capacitor for a square wave
Similarly, how to calculate the reactance for a capacitor when a
  square wave is passing through it. What is the formula?


There isn't a capacitive reactance associated with a square wave.  The very concept of reactance depends on the context of sinusoidal excitation.

When we solve AC circuits in the phasor domain, it is taken for granted that the circuit is in sinusoidal steady state, i.e., all sources are sinusoidal of the same frequency and all transients have decayed away.

That fact is this:  one cannot meaningfully sum phasors or reactances for sinusoids of different frequencies.

Now, that doesn't mean that you can't apply the concept of reactance to find the capacitor voltage across for a square wave current through.

Since (ideal) capacitors are linear, we can decompose the square wave into sinusoidal components, find the associated sinusoidal voltage for each component, and then sum to voltage components to find the total voltage.

Recall the fundamental phasor domain relationship for capacitor voltage and current:

$$\vec V_c = \dfrac{1}{j \omega C}\vec I_c  $$

where \$\omega\$ is the angular frequency of the associated sinusoid.

Now, let

$$i_C(t) = a_1 \cos(\omega t + \phi_1) + a_2 \cos(2\omega t + \phi_2) +  a_3 \cos(3\omega t + \phi_3) + ...$$

For each sinusoidal component, there is an associated phasor.  For example, for the first component, the associated phasor is

$$\vec I_{c_1} = a_1 e^{j\phi_1}$$

Thus

$$\vec V_{c_1} = \dfrac{a_1 e^{j\phi_1}}{j \omega C}$$

so that

$$v_{C_1}(t) = \dfrac{a_1}{\omega C}\cos(\omega t +\phi_1 - \frac{\pi}{2}) $$

Repeat for each term in the series and then sum to find the total capacitor voltage.

Note that we have not defined a reactance to the entire current waveform nor can we define such a thing.  Instead, we

(1) found the reactance to each sinusoidal component

(2) converted each resulting phasor voltage back into the time domain

(3) summed the individual time domain voltage components