NOTE:
The solution cannot be arrays. It is apparently supposed to only include string operators/functions (such as find()) and counter-controlled/sentinel-controlled loops.
I'm working on a project that asks me to code a text editor. The user is prompted to input a sentence, then input a list of commands until they input "end". The program then executes those commands in the order they were typed. For a lot of it, it's not too difficult; I've completed several of the functions necessary for most of the commands the user can input (such as reversing the sentence).
However, here's my problem.
How can I get the user's command inputs to be done sequentially (in the order they're typed) when they input it as a
single string?
As in, rather than doing this:
1 2 3
|
reverse
replace x y
end
|
They do this:
...and the program still calls the reverse function first, then the replace function to replace x with y, and then ends?
From that, I have another question. What if the user doesn't type end in the first string, like this:
|
reverse replace x y reverse
|
It
should allow the user to provide more input, but how could I store previous commands? Like, I get that the sentinel would loop until it read an "end" allowing a new prompt, but how would it store the previous commands (without doing them right away), wait until the user
does input "end", then call all the functions as they were entered?
This is how the output on the screen should look:
1 2 3 4 5 6 7 8 9 10 11 12
|
Enter a sentence:
This is a test. x
reverse replace x y reverse end
//endl
Doing reverse...
x .tset a si shiT
Replacing all x with y...
y .tset a si shiT
Doing reverse...
This is a test. y
Ending...
|
This is how the program is intended to look and execute. I'm still really lost on how to properly allow the user to input the commands in one single string (so, user inputs sentence, then inputs commands all on one line).
Speaking of the replace function (since I feel the answers to these two problems will be similar/the same), much like how I'm confused how a single string can be entered and still be executed sequentially, how do I make the user's input of "replace x y" store the command, *then* store the individual chars as two variables? Like, this might be an elementary question, but how do I separate the three parts when the user is supposed to input them with only single spaces in between?
I can definitely figure out where to go and how to finish it up once I figure out the answers to these questions, but I'm thoroughly stumped. I appreciate any help or guidance. As it is currently, there's a lot of code and it's clunky and messy, but I'll provide snips as best I can very soon (as of posting this).