Implementation of sprintf function