A modified difference scheme for periodic and semiperiodic Sturm-Liouville problems