Write a program that reads two strings stored in two different text files and prints a string containing the characters of each string interleaved. Remove white spaces from both strings before string interleaving. For example, Two strings “Hello World” and “Sky is the Limit” should generate output “HSeklyliosWtohrelLdimit” in python
Code
filename1 = input("Enter file name 1: ")filename2 = input("Enter file name 2: ")# open file in read modef1 = open(filename1, "r")f2 = open(filename2, "r")# read the filedata1 = f1.read()data2 = f2.read()# remove spaces from the stringdata1 = data1.replace(" ", "")data2 = data2.replace(" ", "")# get the length of the stringlength1 = len(data1)length2 = len(data2)max_length = max(length1, length2)# print interleavedfor i in range(max_length):if i < length1:print(data1[i], end="")if i < length2:print(data2[i], end="")print()
Output
Enter file name 1: test.txt
Enter file name 2: test1.txt
HSeklyliosWtohrelLdimit
here test.txt contains Hello World and test1.txt contains Sky is the Limit
0 Comments
Post a Comment