109k views
2 votes
2. Create a file with the follow integer/string content and save it as fun.txt. 6 fun. 3 hello 10 <> 2 25 4 wow! Write an interactive program called PrintStrings that: • Generates file name based on the file path entered by the user • Then read the file fun.txt and produce the following output on the console. fun.fun.fun.fun.fun.fun. hellohellohello <><><><><><><><><><> 2525 wow!wow!wow!wow! Notice that there is one line of output for each integer/string pair. The first line has 6 occurrences of "fun.", the second line has 3 occurrences of "hello", the third line has 10 occurrences of "<>", the fourth line has 2 occurrences of "25" the fifth line has 4 occurrences of "wow!". Notice that there are no extra spaces included in the output. You are to exactly reproduce the format of this sample output. You may assume that the input values always come in pairs with an integer followed by a String (which could be numeric, such as "25" above).

1 Answer

7 votes

Answer:

see explaination

Step-by-step explanation:

I made use of python program to solve this.

text file name with fun.txt.

6 fun. 3 hello 10 <> 2 25 4 wow!

Program code:

import re

file = open('fun.txt','r') #for reading file

strings="" #declaring empty string

for k in file:

strings=strings+k #all character in file is storing in strings variable for do operations

pattern = '\s' #for pattern \s is for space

result = re.split(pattern, strings) #split the string with space

for k in range(len(result)): #loop through the list of string

if(k%2) == 0: #checking for integer to time of string

p=int(result[k])

print(result[k+1] *p) #print times of the string(k+1)

Check attachment for output

2. Create a file with the follow integer/string content and save it as fun.txt. 6 fun-example-1
User Dikuw
by
3.5k points