Hello and thank you for reading. I have written a program to reverse the words in a string using a stack. I am to use 3 sentences and reverse each sentence separately. I have been able to reverse the entire string, which wasn't an issue. Then I changed my program so I am reading to the period and reversing the first sentence. However, I can't get it to read the next sentence. I believe I need a second loop but this is where I struggle. There are several questions/answers on this site that address this assignment, but none that have really taken the approach I have so they aren't relevant. At least, not from what I can tell. This is what I have:
for (String word : wordArray) {
if (word.endsWith(".") {
Stack.push(word.substring(0, word.length()-1));
break;
}
else {
Stack.push(word);
}
}
So my sentences are: "Cats are cool. Dogs are cool. So are turtles." My program will print: "cool are Cats" I know I need to append a period and I can figure that out later. I'm just struggling with how to create a second loop to continue reading the rest of the string. What I need is: "cool are Cats. cool are Dogs. turtles are So."
CodePudding user response:
You were thinking in the right direction: you do need one more loop to empty out the stack when the end of a sentence has been encountered, and you need some mean of storing the data (it could be an ArrayList
, or a StringBuilder
).
I would use a StringBuilder
to store reversed sentences.
The logic of generating a reversed sentence by retrieving the elements from the stack can be extracted into a separate method. In a nutshell, it's while
-loop that runs until the stack is not empty. To join the string back into a sentence, I would use a StringJoiner
which facilitates combining the strings using a specified delimiter (feel free to reimplement this logic as you see fit, the point of this answer is to explain the algorithm and provide a source of inspiration rather than be a ready to go copy-past solution, for instance, you can use two StringJoiner
s instead of the combination StringJoiner
StringBuilder
, I've only shown one possibility).
That's how it might be implemented:
public static String reverseSentences(String[] wordArray) {
Stack<String> stack = new Stack<>();
StringBuilder reversedSentences = new StringBuilder();
for (String word : wordArray) {
if (word.endsWith(".")) {
stack.push(word.substring(0, word.length() - 1));
reversedSentences
.append(createSentence(stack)) // appending the reversed sentence
.append(". "); // adding a period and a white space at the end of the sentence
} else {
stack.push(word);
}
}
return reversedSentences.toString();
}
public static String createSentence(Stack<String> stack) {
StringJoiner sentence = new StringJoiner(" "); // white space would be used a delimiter between the words
while (!stack.isEmpty()) {
sentence.add(stack.pop());
}
return sentence.toString();
}
main()
public static void main(String[] args) {
System.out.println(reverseSentences("Cats are cool. Dogs are cool. So are turtles.".split(" ")));
}
Output:
cool are Cats. cool are Dogs. turtles are So.
CodePudding user response:
Before your loop, you need to create your stack like
Stack<String> wordStack = new Stack<String>();
You would do your current push operations on wordStack,
and then you would pop in your second loop as you would expect.
You got very close.