Doubt in string generation acode

Sir whats wrong in this code why is output wrong

correct way to do is this
look at modified Code