1.One common use of stacks is to parse certain kinds of text strings. Typically
ID: 3539655 • Letter: 1
Question
1.One common use of stacks is to parse certain kinds of text strings. Typically the strings are lines
of code in a computer language, and the programs parsing them are compilers.
Write a program that prompts the user to enter a string, and then checks the delimiters in the
string. The delimiters are the braces '{' and '}', brackets '[' and ']', and parenthesis '(' and ')'. Each
opening or left delimiter should be matched by closing or right delimiter; that is, every '{' should
be followed by a matching '}' and so on. Also, opening delimiters that occur later in the string
should be closed before those occurring earlier. Examples:
c[d] // correct
a{b[c]d}e // correct
a{b(c]d}e // not correct; } doesn't match (
a[b{c}d]e} // not correct; nothing matches final }
a{b(c) // not correct; Nothing matches opening {
The program works by reading characters from the string one at a time and placing opening
delimiters, when it finds them, on a stack. When it reads a closing delimiter from the input, it
pops the opening delimiter from the top of the stack and attempts to match it with the closing
delimiter. If they're not the same type, then an error has occurred.
2.write an algorithm that compresses a string by deleting all space characters in the string.
One way to do so is to use a queue of characters. Insert nonspace characters from the string
into the queue. When you reach the end of the string, dequeue the characters from the queue
and place them back
Given a queue of integers, write an algorithm that, using only the queue ADT, calculates and
prints the sum and the average of the integers in the queue without changing the contents of the
queue.
Given a queue of integers that deletes all negative integers without changing changing the
order of the remaining elements in the queue.
Explanation / Answer
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import java.util.zip.*; public class zipUtil{ public static String compress(String str){ if (str == null || str.length() == 0) { return str; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(str.getBytes()); gzip.close(); return out.toString("ISO-8859-1"); } public static void main(String[] args) throws IOException { String string = "admin"; System.out.println("after compress:"); System.out.println(ZipUtil.compress(string)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.