Java Reference
In-Depth Information
LISTING 13.4
continued
// Print word in reverse
while (!word.empty())
System.out.print (((Character)word.pop()).charValue());
System.out.print (" ");
index++;
}
System.out.println();
}
}
OUTPUT
Enter the coded message:
artxE eseehc esaelp
The decoded message is:
Extra cheese please
A message that has been encoded has each individual word in the message
reversed. Words in the message are separated by a single space. The program uses
the Stack class to push the characters of each word on the stack. When an entire
word has been read, each character appears in reverse order as it is popped off
the stack and printed.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 13.10 How is a queue different from a list?
SR 13.11 Show the contents of a queue after the following operations are per-
formed. Assume the queue is initially empty.
enqueue (5);
enqueue (21);
dequeue();
enqueue (72);
enqueue (37);
enqueue (15);
dequeue();
 
Search WWH ::




Custom Search