Interleave String
Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a string is longer than the other, append the additional characters onto the end of the merged string.
input
the first line of input will containing a string.
the second line of input will containing a string.
the strings may contain special characters ,digits and string.
output
the output should be the merged string
explanation
For example, if the given strings are "abc" and "pqrs", then alternating characters from each string are merged as "a" from "abc", "p" from "pqr" "b" from "abc" and so on ...., resulting in the merged string "apbqcr".
sample input 1
abc
pqr
sample output 1
apbqcr
sample input 2
abc
pqrst
sample output 2
apbqcrst
first_line = input("Enter the first line: ")
second_line = input("Enter the second line: ")
result_line = ''
k_f = 0
k_s = 0
for i in range(0, len(first_line) + len(second_line)+2, 2):
if k_f <= len(first_line)-1:
result_line += first_line[k_f]
if k_s <= len(second_line)-1:
result_line += second_line[k_s]
k_f += 1
k_s += 1
print(result_line)
Comments
Leave a comment