A Periodicity Theorem on Words and Applications