Find the first repeated word in a given string. 

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
  1. Take a string as input and create an empty list.
  2. Add each word from the string to the empty list using for loop.
  3. If a word repeats then break the loop.
  4. 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
				
			

split a string on the last occurrence of the delimiter. 

find the second most repeated word in a given string

Leave a Comment