Interleave Strings | Python Program to merge two strings by adding characters in alternating order

 Interleave Strings

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 contain a string.

The second line of input will contain a string.

The strings may contain special characters, digits and letters.

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

Code:

word1=input()
word2=input()

word1Len=len(word1)
word2Len=len(word2)

if word1Len>word2Len:
    commonLen=word2Len
else:
    commonLen=word1Len

newString=""

for i in range(commonLen):
    j=word1[i]+word2[i]
    newString+=j

if commonLen==word1Len:
    result=newString+word2[commonLen:]
else:
    result=newString+word1[commonLen:]

print(result)

Comments

Popular posts from this blog

CODING ASSIGNMENT 4

CODING PRACTICE 11 Music Page

CODING ASSIGNMENT 3