Tuesday, October 14, 2008

parsing a string

study:

You are to create a console application that accepts exactly one command-line argument. If it doesn’t receive the argument, the application must display an error message and exit. The application must parse the text input and output the number of times each letter of the alphabet occurs in the text. Case sensitivity is not required.

For example, if the command-line argument is “baaad” the displayed result must be:

There are 3 A's
There are 1 B's
There are 0 C's
There are 1 D's
There are 0 E's
There are 0 F's
etc...


Result: In this we use Stream Tokenizer.


mport java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;


public class Class1 {

public static void main(String[] av) throws IOException {
StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
String s = null;
char a[] = {'A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z'};

int count = 0;
int i, r=0,m=0;

while ((i = tf.nextToken()) != StreamTokenizer.TT_EOF) {
switch (i) {
/* case StreamTokenizer.TT_EOF:
System.out.println("End of file");
break;
case StreamTokenizer.TT_EOL:
System.out.println("End of line");
break;
case StreamTokenizer.TT_NUMBER:
System.out.println("Number " + tf.nval);
break;*/

case StreamTokenizer.TT_WORD:
s = tf.sval.toUpperCase();
System.out.println("Word, length " + tf.sval.length() + " " );
while(r<>

{if (s.charAt(p) == a[r])

count++;

}

System.out.println("There are " +count +a[r]);

count=0; r++; }

break;

default:

System.out.println("What is it? i = " + i);

} } } }

No comments: