When King Dedede attacks with a hammer, he produces a star on impact. Suck up that star; it's the only way to fight back. However, don't immediately spit it out: if Dedede decides to repeatedly pound the ground with the hammer, spitting the star back at him immediately usually results in it hitting his hammer, which blocks it. Keep the star in Kirby's mouth for a moment until you're sure Dedede's attack has stopped, then spit it at him.