It fails as u might have not modified
scanf("%s",molecule);

Even here u'll need to consider the same

scanf("%s%*c",molecule);

Use the above method if u still wish to use scanf...

Else u can use fgets instead as suggested by xpi0t0s which would be a better approach