One may describe the waves in terms of $\Delta x$, the deviation of the position of strings' atoms from the equilibrium locations. Because they're attached, $\Delta x(\sigma)=0$ for $\sigma$, the coordinate along the string, equal to any of the end point values, $\sigma=0$ and $\sigma=L$.
But $\Delta x$ obeys a wave equation, so the eigenstates of the frequency have to depend on $\sigma$ as sines and cosines:
I wrote the solution at $t=0$, a moment when $\Delta x $ is nonzero (or maximized).
The condition $\Delta x=0$ at $\sigma=0$ says $B=0$, so we only have signs, and $\Delta x =0$ at $\sigma=L$ implies $kL=n\pi$ because the sine vanishes when the argument is a multiple of $\pi$ i.e. $L=n\lambda /2$ because $k=2\pi/\lambda $. The calculation works both for longitudinal and transverse oscillations $\Delta x$ – but they are usually transverse. (In string theory where this exact same calculation is important for the basics as well, the longitudinal oscillations are absolutely unphysical.)
I reduced the spatial dependence on a sine. The sine is a standing wave – much like a cosine, just a shifted sine – and it may also be understood as an equal mixture of the right-going and left-going (signs) complex wave $\exp(\pm ik\sigma)$.