Consider the initial boundary problem
$$
\left\{
\begin{array}{lll}
&u_{tt}-c^2u_{xx}=F(x,t),\; a<x<b,\; t>0,\\
&\text{homogeneous boundary conditions on } (a,b),\\
&u(x,0)=f(x),\; a<x<b,\\
&u_t(x,0)=g(x),\; a<x<b.
\end{array}
\right.
\qquad(1)
$$
Solving steps:
Solve eigenvalue problem
$$-X_k''=\lambda_k X_k,$$
with homogeneous boundary conditions on $(a,b)$.
Find Fourier series expansions
$$F=\sum F_kX_k,$$
$$f=\sum f_kX_k,$$
$$g=\sum g_kX_k.$$
Solve ODE problems
$$T''_k(t)+c^2\lambda_kT_k(t)=F_k,\; T_k(0)=f_k,\;T'_k(0)=g_k.$$
Then solution of $(1)$ is
$$u=\sum T_k(t)X_k$$
For example, consider the problem(Pinchover and Rubinstein, An introduction to partial differential equations, p. 114, section 5.4)
\begin{equation}
\begin{split}
&u_{tt}-u_{xx}=\cos2\pi x\cos2\pi t,\; 0<x<1,\; t>0,\\
&u_x(0,t)=u_x(1,t)=0,\;t\ge0,\\
&u(x,0)=\cos^2\pi x,\; 0\le x\le 1,\\
&u_t(x,0)=2\cos2\pi x,\; 0\le x\le 1.\\
\end{split}
\end{equation}
- Solutions of eigenvalue problem
$$-X_k''=\lambda_k X_k,\;X'_k(0)=X'_k(1)=0$$
is
$$X_k(x)=\cos(k\pi x),\quad \lambda_k=(k\pi)^2,\quad k=0,1,2,\dots$$
- $F=\cos2\pi x\cos2\pi t$. Then
$$F_k=\begin{cases} \cos2\pi t \text{ if } k=2, & \\ 0 \text{ if } k\neq2. \end{cases}$$
$f=\cos^2\pi x=\frac12+\frac{\cos2\pi x}{2}$. Then
$$f_k=\begin{cases} \frac12 \text{ if } k=0\quad \text{or} \quad k=2, & \\ 0 \text{ in other cases} \end{cases}$$
$g=2\cos2\pi x$. Then
$$g_k=\begin{cases} 2\text{ if } k=2, & \\ 0 \text{ if } k\neq2. \end{cases}$$
- If $k=0$ then $X_0=1$, $\lambda_0=0$, ODE problem is
$$T''_0(t)=0,\; T_k(0)=\frac12,\;T'_k(0)=0.$$
If $k=2$ then $\lambda_2=4\pi^2$, ODE problem is
$$T''_2(t)+4\pi^2T_2(t)=\cos2\pi t,\; T_2(0)=\frac12,\;T'_2(0)=2.$$
We obtain $$T_0(t)=\frac12,$$
$$T_2(t)=\frac{\left( t+4\right) \sin{ 2\pi t }}{4\pi }+\frac{\cos{ 2\pi t }}{2},$$
$$T_k(t)=0 \text{ if } k\neq0,\;2.$$
- Final solution is
$$u=T_0(t)X_0+T_2(t)X_2=
\frac12+\left(\frac{\left( t+4\right) \sin{ 2\pi t }}{4\pi }+\frac{\cos{ 2\pi t }}{2}\right)\cos 2\pi x$$