SPECS ===== *** Reprinted from: *** Association for Computing Machinery, 2009. C
ID: 3624049 • Letter: S
Question
SPECS=====
*** Reprinted from:
*** Association for Computing Machinery, 2009.
Character Checker
Write a program that will report on the characters in a file
(which is either text or binary, so you should code for binary).
The characters are to be split into the following classes based on their
eight-bit values (shown here in hexadecimal):
non-space control:
0x00 through 0x1F, but only those which are not also whitespace
0x7F delete
whitespace:
0x20 space
0x09 tab
0x0A newline
0x0B vertical tab
0x0C formfeed
0x0D carriage return
non-space printable:
0x21 through 0x7E
non-ascii:
0x80 through 0xFF
Input to your program will be a file of eight-bit bytes.
Your program is to print the number of characters in the file by printing a
line containing the string "total:", a single space, followed by the count
with no leading zeroes. If the count is non-zero print a detail
report on the characters seen in the file.
In the detail report represent an eight-bit character by a two-place
hexadecimal value, using capital letters for the
(hexadecimal) values A through F with a leading zero if necessary.
For example, the character "n" would be 6E.
Report the last character seen before the end of the file by printing
"last:", a single space, and the hex representation of the character.
Next, report the count of characters in each class, in the order:
non-space control, whitespace, non-space printable, and non-ascii.
Print the class name, a colon, a single space, and the count without
leading zeroes.
If the whitespace count is non-zero report the encountered whitespace
characters immediately after the whitespace
report line. For each whitespace character that appears at
least once in the file print two spaces, the hex representation of the
character, a single space, and the count without leading zeroes. Print one
line per character, in character numerical order.
No trailing whitespace is to appear on an output line.
SAMPLE INPUT FILE:
(The ---'s mark the top and bottom of the file, but are not in the file.)
---
this is a sample
---
OUTPUT FOR THE SAMPLE INPUT:
total: 17 [or 18, on some non-Unix systems]
last: 0A
non-space control: 0
whitespace: 4 [or 5, on some non-Unix systems]
0A 1
20 3 [or 1 extra line, on some non-Unix systems]
non-space printable: 13
non-ascii: 0
To input the information in binary files (or text files),
with each item read in as one byte (so, essentially, an integer in the
range 0-255 inclusive), reading up to "end of file", use this code structure:
import java.io.*;
public class Prog1
{
public static void main(String args[]) throws IOException
{
BufferedInputStream in = new BufferedInputStream(System.in);
int ch;
while ( (ch=in.read()) != -1 ) //loop reads until "end of file"
{
//ch is now the next byte value, 0-255, in the file
}
HINTS
=====
* It's OK to use arrays for your info storage, if you prefer.
* Java has a simple command to convert an integer into hexadecimal (String):
String s = Integer.toString(c,16);
where c is some integer. But, the hex digits A to F are given in lowercase.
* Java's s.toUpperCase() command is useful.
(It computes a new String, it does not change s.)
Explanation / Answer
The program needs to be given a path to a file as an argument. When I ran this on the sample file I got the desired output except for an extra carriage return (0D). This is only because of my "non-Unix system" though.
import java.io.*;
public class Prog1 {
public static void main(String args[]) throws IOException
{
String filename = args[0];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
int ch;
int total=0;
int nonAscii = 0;
int last=0;
int[] whiteSpaceChars = new int [] {0x09,0x0A,0x0B,0x0C,0x0D,0x20};
int[] whiteSpaceCount = new int[256];
int allWhiteSpace = 0;
int nonSpaceControl = 0;
int nonSpacePrintable = 0;
while ( (ch=in.read()) != -1 ) //loop reads until "end of file"
{
total+=1;
last = ch;
if (ch >= 0x80) {
nonAscii += 1;
}
//ch is now the next byte value, 0-255, in the file
if (ch <= 0x20 || ch == 0x7f) {
nonSpaceControl += 1;
}
if (ch >= 0x21 && ch <= 0x7e) {
nonSpacePrintable += 1;
}
for (int whiteSpaceChar : whiteSpaceChars) {
if (ch == whiteSpaceChar) {
whiteSpaceCount[ch] += 1;
allWhiteSpace += 1;
nonSpaceControl -= 1;
}
}
}
System.out.println("total: "+total);
System.out.format("last: %02X ",last);
System.out.println("non-space control: "+nonSpaceControl);
System.out.println("whitespace: "+allWhiteSpace);
for (int whiteSpaceChar : whiteSpaceChars) {
if (whiteSpaceCount[whiteSpaceChar] > 0){
System.out.format("%02X %d ",whiteSpaceChar,
whiteSpaceCount[whiteSpaceChar]);
}
}
System.out.println("non-space printable: "+nonSpacePrintable);
System.out.println("non-ascii: "+nonAscii);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.