In this program, we will take a string as input and find the first repeated word in a given string.
Steps to solve the program
- Take a string as input and create an empty list.
- Add each word from the string to the empty list using for loop.
- If a word repeats then break the loop.
- Print the output.
#Input string
str1 = "ab bc ca ab bd ca"
temp = []
for word in str1.split():
if word in temp:
print("First repeated word: ", word)
break
else:
temp.append(word)
Output :
First repeated word: ab