211k views
2 votes
Write a function remove_italicized_text that takes one parameter, a string named sentence. The string might contain HTML tags for representing italicized text. An HTML tag is a kind of code that directs the web browser to format content in a particular way.

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

def remove_italicized_text(x):

ans = ""

n = len(x) # length of string

i = 0

while i < n:

if x[i:(i+3)] == "<i>": # check for opening italic html tag

i += 3

cur = ""

while i < n:

if x[i:(i+4)] == "</i>": # check for closing italic html tag

break

cur += x[i]

i += 1

i += 4

ans += cur # add the text between two tags into output string

ans += " " # adding a space between two next

ans += cur # add the text again

else:

ans += x[i]

i += 1

return ans

sentence = "<i>Stony Brook</i>"

print("sentence = " + sentence)

print("Return value: " + remove_italicized_text(sentence))

print("\\")

sentence = "I <i>love</i> SBU, yes I do!"

print("sentence = " + sentence)

print("Return value: " + remove_italicized_text(sentence))

print("\\")

sentence = "Hey <i>Wolfie</i>, he's so fine, we hug him <i>all the time</i>!"

print("sentence = " + sentence)

print("Return value: " + remove_italicized_text(sentence))

print("\\")

sentence = "This text has no italics tags."

print("sentence = " + sentence)

print("Return value: " + remove_italicized_text(sentence))

print("\\")

User Crowley
by
5.0k points