ImplementStackADTReverseSentences
- Info
- Hidden Rankings
Stack ADT Application in Reversing Words in a Sentence
Problem Description
Code a program from scratch that reverses the words in a sentence.
Input: Give a sentence that has no punctuation other than letters and spaces
Output: Reverse the words in a sentence
Example:
Input
Goodrich and Tamassia and Goldwasser Book on Data structures and algorithms in Python
Output
Python in algorithms and structures Data on Book Goldwasser and Tamassia and Goodrich
Implement using a stack
Steps:
1) Read characters in a string and place them in a new word.
2) When we get to a space, push that word onto the stack, and reset the word to be empty.
3) Repeat until we have put all the words into the stack.
4) Pop the words from the stack one at a time and print them out.
A Sample Prototype
include
include "console.h"
include "stack.h"
using namespace std; const char SPACE = ' ';
int main() { string sentence = "INPUT"; string word;
cout << "Original: " << sentence << endl;
for (char c : sentence) {
if (c == SPACE) {
push(word);
} else {
word += c;
}
}
if (word != "") {
push(word);
}
cout << " Output: ";
while (isEmpty()) {
word = pop();
cout << word << SPACE;
}
cout << endl;
return;
}
Problems
Problem | Points | AC Rate | Users | |
---|---|---|---|---|
StackADTReverseSentences | 5 | 49.7% | 67 | |
Stack Implementation Basics | 10 | 0.0% | 0 | |
Stacks- Basics | 15p | 14.6% | 122 |
Comments