I would use strtok
to split the input string into words.
Then, I will add each word to a hashtable: the keys would be the words, and the values would be the word's first occurrence position.
When inserting a new word into the hashtable, if the world is already there, then I'd compute the distance between the current position and that word's position.
Hope it helps.