I need a systematic way of replacing each word in a string separately by providing my own input for each word. I want to do this on the command line.
So the program reads in a string, and asks me what I want to replace the first word with, and then the second word, and then the third word, and so on, until all words have been processed.
The sentences in the string have to remain well-formed, so the algorithm should take care not to mess up punctuation and spacing.
Is there a proper way to do this?
Given some text
You first tokenize the string into words and “everything else” tokens (e.g. call them fill).
Regular expressions are helpful for that:
The job is now to convert the return value into a more useful data-structure, like an array of tokens and an index of all words used:
Exemplary you can then output all words:
Which would give you with the sample text:
Then you do the job on the word tokens only. For example replacing one string with another:
Finally you concatenate the tokens into a single string:
Which gives with the sample text again:
Job done. Ensure that word tokens are only replaced with valid word tokens, so tokenize the user-input as well and give errors if it’s not a single word token (does not matches the word pattern).
Code/Demo