String and loops

Thank you for your response. I will try the code to derive the desired output and will get back.